• Pete

    (@perthmetro)


    I’m trying to find a way to add a body class “author” IF the current user is the author of the post they are viewing.

    This is what I have so far…

    add_filter( 'body_class','my_body_classes' );
    function my_body_classes( $classes ) {
     
        if ( !$current_user->ID == $post->post_author ) {
        
            $classes[] = 'post-author';
             
        }
         
        return $classes;
         
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I’d use get_current_user_id()

    add_filter( 'body_class','my_body_classes' );
    function my_body_classes( $classes ) {
        global $post;
        $cur_user = get_current_user_id();
        if ( $cur_user == $post->post_author ) {
                $classes[] = 'post-author';
        }
        $classes[] = 'cur_user_id_' . $cur_user;
        return $classes;
         
    }

    I added the 2nd body class for purposes of debugging and explicitly declared $post as a global. Of course, this only works if the post’s author is logged in.

    Thread Starter Pete

    (@perthmetro)

    Thanks very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add “author” to the body class of a post’ is closed to new replies.