WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display author gravatar within the loop (6 posts)

  1. mars-hill
    Member
    Posted 6 years ago #

    Hi,

    I've been trying to use get_avatar to display the author's gravatar under each post on the Indie Travel Podcast.

    This is the code I've been using in single.php:

    <?php echo get_avatar( $id_or_email = '<?php the_author_email(); ?>', $size = '80', $default = 'http://indietravelpodcast.com/images/no_gravatar.jpg' );
       ?>
    
    		<p class="authorprofile"><?php the_author_description(); ?> </p>

    Unfortunately the following line doesn't work: everything else is fine. This code currently displays the default image.

    $id_or_email = '<?php the_author_email(); ?>'

    Can anyone please suggest the right value for id_or_email?

    Thanks in advance, mars-hill

  2. nickohrn
    Member
    Posted 6 years ago #

    Try the following:

    <?php
    echo get_avatar( the_author_email(), '80', 'http://indietravelpodcast.com/images/no_gravatar.jpg');
    ?>
  3. nickohrn
    Member
    Posted 6 years ago #

    Forgot to mention why what you were doing wouldn't work. To do parameter passing in PHP, you don't need to include the parameter name. So if you see a function signature like this:

    <?php
    function my_sweet_function( $arg, $second_arg ) {
        // Do stuff
    }
    ?>

    To call it, all you need to do is say

    my_sweet_function( 'my arg', 3 );
  4. Ian Stewart
    Theme Wrangler
    Posted 6 years ago #

    You need to use get_the_author_email(). Like,

    <?php echo get_avatar( get_the_author_email(), '80', 'http://example.com/no_avatar.jpg' ); ?>

  5. mars-hill
    Member
    Posted 6 years ago #

    Thanks so much guys: it's much appreciated. I've learned HTML and CSS to a reasonable standard, but I'm playing PHP by ear.

    It was necessary to use get_the_author_email, as Iandsetward said.

    All is now fine in the land of the Indie Travel Podcast!

  6. nickohrn
    Member
    Posted 6 years ago #

    Thanks for pointing that out iandstewart. I forgot that most of the common template tags just print out the information rather than returning it.

Topic Closed

This topic has been closed to new replies.

About this Topic