0

I am trying to add avatars to my wordpress web site and I activated them through the settings - discussion as you would normally do. But because I have a very customised theme, I guess, it is not working.

That is the code in my functions.php, where it shows the author and the comment, but not the avatar/gravatar:

function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
 <div id="comment-<?php comment_ID(); ?>">
  <div class="comment-author vcard">
     <?php printf(__('%s <span class="says">:</span>'), get_comment_author_link()) ?><br />
            <?php if ($comment->comment_approved == '0'): ?>
    <em><?php _e('Your comment is awaiting moderation.') ?></em>
    <br />
            <?php endif; ?>
  </div>

  <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ) ?>">
<?php printf(__('%1$s, %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div>

  <?php comment_text() ?>

Here is an example how comments are shown right now: http://goo.gl/QkSzrd

I believe, I have to build in get_avatar somewhere in the line before "get_author_link", but everything I did just lead to failure. Either it was just showing the "missing person" avatar (and not the ones connected to those commenting users) or it was deleting the author's name and showing nothing. :\

UPDATE:

Changing the line

     <?php printf(__('%s <span class="says">:</span>'), get_comment_author_link()) ?><br />

to

     <?php printf(__('%s <span class="says">:</span>'), get_avatar ($comment, '72')) ?><br />

brings up the avatar icon, I think, but it deletes the author/username of the comment and it places the avatar some place, I cannot understand (right side corner).

I can live with the right side corner, maybe it is just a CSS issue, but how do I get author/username of the comment back before the colon? In other words: I would like to have both, get_comment_author_link and the avatar.

  • You should use esc_url instead of htmlspecialchars to escape your comment link, and you should escape your comment time date and author link, glad to see someone thinking about escaping though :) – Tom J Nowell Jul 17 '15 at 21:18
  • have you searched the WordPress COdex codex.wordpress.org/Function_Reference/get_avatar ? – Michael Jul 17 '15 at 21:22
  • Tom, do you mean ... echo esc_url( get_comment_...? And where to add that on time, date and author link? Can you write it down? Thanks, mate. :) – vega Jul 17 '15 at 22:03
  • Thanks, Michael, as written in my post, I know I have to use the get_avatar function, but I could not get it working. – vega Jul 17 '15 at 22:04
1

You're looking for the get_avatar function, documented here

get_avatar( $id_or_email, $size, $default, $alt )

You'll need either a user ID, or an email to use it, something like this should do the trick:

echo get_avatar( get_comment_author_email(), 'thumbnail' );

If avatars are turned off in settings, this function will not return anything

  • Thanks, Tom, can you please read my update? I changed something, which still does not work properly. – vega Jul 17 '15 at 22:22
  • You removed the comment author link and replaced it with the avatar function, and now your author link has vanished and been replaced with an avatar, I'd say that's why. If you want both, you need to output both. However that's a standard PHP printf question, not a WordPress question. – Tom J Nowell Jul 18 '15 at 1:12
0

Alright, I just doubled it now by adding the avatar line before the author link. Both keep intact this way and get to be shown.

Here is the code, I added/changed:

<div class="comment-author vcard">
  <?php echo get_avatar ($comment, '72'); ?>
  <?php printf(__('%s <span class="says">:</span>'), get_comment_author_link()) ?><br />
        <?php if ($comment->comment_approved == '0'): ?>
  <em><?php _e('Your comment is awaiting moderation.') ?></em>
  <br />
        <?php endif; ?>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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