• Resolved mars-hill

    (@mars-hill)


    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 = 'https://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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try the following:

    <?php
    echo get_avatar( the_author_email(), '80', 'https://indietravelpodcast.com/images/no_gravatar.jpg');
    ?>

    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 );

    You need to use get_the_author_email(). Like,

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

    Thread Starter mars-hill

    (@mars-hill)

    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!

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display author gravatar within the loop’ is closed to new replies.