4

I need to display author image along with posts. I used the following code to display posts with author image.

$the_query = new WP_Query("post_type=post&paged=".get_query_var('paged'));
while ( $the_query->have_posts() ) : $the_query->the_post();

<?php the_excerpt(); ?>
<p><?php the_author_image(); ?></p>

<?php endwhile; ?>

But the author image didn't display. I couldn't track the error. How can i do this?

2
  • I don't think the_author_image() is a Wordpress defined function: there is no place to set an image in the edit user profile area of the dashboard. Looks like you need this plugin: wordpress.org/extend/plugins/sem-author-image Otherwise, you would need to upload images of the authors, add a custom field to the user meta table that stores the "attachment" post for that author, then get the meta field data and output it as an <img> tag where you have the_author_image() in the above code.
    – miahelf
    Commented Apr 17, 2012 at 7:38
  • you're right. I am using author image plugin for upload author image. If i use the code "while ( have_posts() ) : the_post();" the author image displayed correctly. But i have to use the code "while ( $the_query->have_posts() ) : $the_query->the_post();". Could i get this correctly using the second code? Commented Apr 17, 2012 at 7:45

4 Answers 4

8

I know this is old, but I just came across it shortly before I came across the solution.

To display the author image inside the loop, just use this code:

<?php echo get_avatar( get_the_author_meta( 'ID' ) , 32 ); ?>

Where '32' is the size of the image. If it's outside the loop, then just specify the author's user ID.

1
  • 1
    +1 Thanks for the late answer! It's always beneficial to the community when questions are fleshed out even after they've become stagnant. Commented Sep 21, 2012 at 1:50
2

If you are not using it inside a normal loop, get the author ID a different way and use the function like this:

the_author_image($authorid);

Basically you need to supply the author ID to the function if it is not in a proper loop. If you are having trouble getting the ID from your query let me know.

4
  • I got the author id correctly but the image didn't display. Commented Apr 17, 2012 at 8:00
  • Can you try hard coding one of the author id's like this and let me know what happens: the_author_image(5);
    – miahelf
    Commented Apr 18, 2012 at 0:09
  • 1
    I just used the query post instead of new wp query and the code "the_author_image($authorid);" works fine for me. Anyway thank you so much for your continuous effort Commented Apr 19, 2012 at 4:24
  • If you found the solution to your problem, go ahead and post the solution as an answer and accept it, or if you think my post clarified what you need you can accept it instead.
    – miahelf
    Commented Apr 19, 2012 at 7:14
0

Note the answers here are for the built-in Gravatar photo. If you want to choose/set photos for your authors you'll need a plugin or write your own as miahelf mentions.

0

Upload a specific image for author requires 2 things:

  • extend the get_the_author_meta() function
  • link the implementation of the extension to media file management system in WP

Please Note that get_the_author_image() is deprecated and will not work for WP > 3.0

1
  • This is a quite vague answer...
    – kaiser
    Commented Dec 2, 2013 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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