WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to add a custom body class to WordPress which should be the author of the post which is displayed. I looked at different how to's and tried to create some code but it doesn't work:

add_filter( 'body_class' , 'ft_add_guest_body_class' );

function ft_add_guest_body_class( $classes ) {

    // add 'wp_admin' to the $classes array

    if (get_the_author() != 'MrX')
        $classes[] = 'wp_admin';

    // return the $classes array
    return $classes;


The problem is that the wp_admin body class is displayed on every page and post. When I remove the conditional the body class is still applied so it must be that.

share|improve this question

migrated from stackoverflow.com Jan 20 '12 at 14:20

This question came from our site for professional and enthusiast programmers.

Do some debugging then. What does get_the_author() return? – Pekka 웃 Jan 20 '12 at 11:26
it actually returns nothing... – Thomas Jan 20 '12 at 13:12
that's your problem then. I think this is because the post hasn't been loaded yet. Not sure what the best is to deal with this... I'll flag this for migration to Wordpress.SE, the guys there will know – Pekka 웃 Jan 20 '12 at 14:11
Beware the PHP voodoo. – Raphael Jan 20 '12 at 17:40
$author = $wp_query->post->post_author;
$author = get_user_by('id', $author);

That should get you the author slug which you can use in your conditional statement. get_the_author can only be used inside the loop.

share|improve this answer

It's because you're trying to use get_the_author() outside of the Loop. Try using $post->post_author instead, which will return the author's ID for the current post.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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