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.

Hi I'm try to get an if statement to work.

I want to display some HTML code if the post is created by a specific author.

This is what I'm come up with but it doesn't work.

<?php if ( get_posts ($post->ID, 'post_author', true) == '20')
  { ?>  HTML <?php } ?> 

Thank You

share|improve this question
1  
In what context is this code to be used? Inside the loop? Outside the loop? –  Chip Bennett Nov 27 '12 at 17:10
add comment

2 Answers

The get_posts() function retrieves posts from the database. If I understand you correctly, that's not what you want though, you simply mean to check the author of the post currently displayed.

You can simply use $post->post_author. If outside the Loop, you have to call upon the $post global first.

Hence

global $post; // only required outside the loop

if ( 20 === $post->post_author ) {
    // do stuff
}

will do what you want.

share|improve this answer
    
Thanks Johannes that worked just need to add the ' to 20 to get '20' –  user24126 Nov 28 '12 at 15:54
    
The $post_author property of the $post object should be an integer and not a string, hence 20 should do. If you experience it differently though, then you must be right in adding the apostrophes. Alternatively, you could use == instead of === as a comparison operator, which is less strict does not check for variable type (20 == '20' returns true whereas 20 === '20' returns false). If this helped you, you could mark this answer as correct (checkmark on the left hand). –  Johannes Pille Nov 28 '12 at 16:06
add comment

Assuming you're inside the loop, you can use get_the_author(), which returns the Author Display Name:

if ( 'Author Display Name' == get_the_author() ) {
    // This post is written by the
    // specified author; do something
}

Note that get_the_author() accepts no parameters, and as such must be used from inside the Loop.

share|improve this answer
add comment

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.