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;


1 Answer 1


The code isn't working because you haven't defined or retrieved the $current_user or $post variables from anyway. You've also got a ! here for some reason: !$current_user->ID, which will just break the condition.

You need to use the appropriate functions to get their values, and also use is_single() to make sure you're viewing a single post (otherwise the post author could be missing or something unexpected).

    function( $classes ) {
        if ( is_single() ) {
            $post = get_queried_object();
            $user = wp_get_current_user();

            if ( $user->ID == $post->post_author ) {
                $classes[] = 'post-author';

        return $classes;
  • Whoops, sorry about the !. Thanks for the showing me how to retrieve the user and post :)
    – Pete
    Jul 27, 2019 at 7:54
  • 1
    Fixed the answer, last } should've been ); Jul 27, 2019 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.