Support » How-To and Troubleshooting » Using a gravatar image within author.php

Using a gravatar image within author.php

  • I run a blog with multiple authors. Recently I added a custom “author.php” template to the site, which is supposed to include an author photo, bio and all their posts. For the photo, I figured I’d use gravatars. Here’s the code I tried:

    <?php while (have_posts()) : the_post(); ?>
    <?php echo get_avatar( get_the_author_email(), '56' ); ?>
    <?php endwhile; ?>

    Unfortunately that outputs about 10 gravatar images, one for each post I’ve made. So I added a query_posts function with “showposts=1”, and thankfully it now displays only one gravatar — but it’s the gravatar for one of my other authors, not mine!

    Any obvious coding that would get this to work correctly?

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’m having similar difficulties. When placed outside of the loop, the gravatar code returns an incorrect image, but when placed inside the loop, it attaches a gravatar to each post listed for that author.

    The code below is what I originally tried, which returns the incorrect gravatar.

    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    	<div id="content" class="narrowcolumn">
    if(isset($_GET['author_name'])) :
    $curauth = get_userdatabylogin($author_name);
    else :
    $curauth = get_userdata(intval($author));
    <h2><?php echo $curauth->display_name; ?></h2>
    <span class="alignright" style="margin-left:1em;"><?php echo get_avatar( get_the_author_email(), $size = '96' ); ?></span>
    <dt>About the author</dt>
    <dd><?php echo $curauth->user_description; ?></dd>
    <h4>Posts by <?php echo $curauth->display_name; ?></h4>
    <!-- The Loop -->
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
    <?php the_title(); ?></a>,
    <?php the_time('d M Y'); ?> in <?php the_category('&');?>
      <?php endwhile; else: ?>
         <p><?php _e('No posts by this author.'); ?></p>
    	<?php endif; ?>
    <!-- End Loop -->
    <?php get_footer(); ?>

    I tried $curauth->gravatar, which I suspected would not work (and didn’t). I even downloaded the Gravatar plug-in, hoping that would allow me to place the code outside of the loop; but that failed as well.

    I think with a bit of scouring of these forums I’ve managed to solve this problem. I have author gravatar images outside the loop on the author.php page followed by a list of all the author’s posts by using this code:

    <?php global $authordata, $curauth;
    		$authordata=get_userdata(get_query_var( 'author' )); if(function_exists('get_avatar')) { echo get_avatar( get_the_author_id(), 70, "#646464" ); } ?>
    		<h2 class="grey"><?php echo $curauth->display_name; ?></h2>
    		<a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a>
    		<p><?php echo $curauth->user_description; ?></p>

    Hope that works for you too.

    Worked for me. Thanks.

    Thanks SmallBiz that worked for me as well.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using a gravatar image within author.php’ is closed to new replies.