Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to show something in the sidebar of a post only if the author of the post equals a specific author or username. I was looking at is_author() but that doesn't seem to do the trick.

share|improve this question

2 Answers 2

Try this:

/**
 * Do the following only on Single post page
 */
if( is_single() ){
    global $post;

    $specific_username = 'fred';

    $post_author_id = $post->post_author;
    $post_author_username = get_the_author_meta('user_login', $post_author_id );

    if( $post_author_username == $specific_username ){
        //SHOW SOMETHING
    }
}
share|improve this answer

You can access the posts author (inside the loop) with the global $post;

echo $post->post_author

If your sidebar comes after your main content (in code), then you could populate a global array (not the best solution, but ok).

// in the loop: Add each author to the global $post_authors array
$post_authors[] = $GLOBALS['post']->post_author;

// in your sidebar:
if ( in_array( 'Author Name', $GLOBALS['post_authors'], true ) )
{
    // do stuff
}
share|improve this answer
    
I can't this to work. I replace 'Author Name' with the author Im trying to target but it doesn't return anything on the posts by that author. –  Pollux Khafra Apr 4 '12 at 18:35
    
what? Please post some sample code in your answer. I don't get a single word. –  kaiser Apr 4 '12 at 20:02
    
I used your snippet and replaced Author Name with the name of an author from my site. I placed an element inside the brackets but it doesnt return anything when the Author Name is equal to the author name of the post. –  Pollux Khafra Apr 4 '12 at 20:11
    
Shure, that the Author Name is really the authors name and not the display_name, first_name, user_login, etc? –  kaiser Apr 4 '12 at 20:55
1  
var_dump($post_authors[] = $GLOBALS['post']->post_author); <- don't do this. Instead please spend 2 (not more needed) hours in front of the manual. 2 of your questions (incl. comments and your testing) waste more time than you'd need there. The sys is already at the point that it tells us to stop writing here and move on in chat > flag poppin' up for mods. More wasted time. So it's not pissed, it's part of my answer: Spend time on the manual or lower your expectations or ask (for yourself) easier Qs. If you're not doing one of those, you won't get satisfying answers. –  kaiser Apr 5 '12 at 1:15

Your Answer

 
discard

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.