WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] A little theme function help (get_avatar) (3 posts)

  1. Jeremy Clark
    Moderator
    Posted 6 years ago #

    Okay I'm by no means a coder and this will probably be pretty funny to somebody but here is what I'm trying to do. I'm adding the new get_gravatar function to my themes and this is the code that I'm using.

    function techozoic_gravatar() {
    if (function_exists('get_avatar')) : ?>
    <div class="avatar_cont">
    <?php if (! empty($comment->comment_author_url) ){
    // Did they leave a link ?>
           <a rel="external nofollow" href="<?php comment_author_url(); ?>" title="<?php comment_author(); ?>">
           <?php echo get_avatar( get_comment_author_email(), '50' ); ?></a>
        <?php } else { ?>
           <?php echo get_avatar( get_comment_author_email(), '50' ); }?>
           </div>
    <?php endif; } ?>

    I've put this in my functions.php and calling it in my comment loop with <?php techozoic_gravatar(); ?> . The thing is it seems that in the functions.php file the $comment->comment_author_url doesn't work, but if I put the code directly into my comment loop it works just fine. Thanks

  2. Jeremy Clark
    Moderator
    Posted 6 years ago #

    It never fails that as soon as I post I get it figured out. I forgot to declare the $comment as global. Here is the new code. What this code does is it will also link the gravatar image to the url that they provide in the comment. But if no url is given it doesn't create a empty <a href> tag.

    function techozoic_gravatar() {
    if (function_exists('get_avatar')) : ?>
    <div class="avatar_cont">
    <?php global $comment;
    if (! empty($comment->comment_author_url) ){
    // Did they leave a link ?>
           <a rel="external nofollow" href="<?php comment_author_url(); ?>" title="<?php comment_author(); ?>">
           <?php echo get_avatar( get_comment_author_email(), '50' ); ?></a>
        <?php } else { ?>
           <?php echo get_avatar( get_comment_author_email(), '50' ); }?>
           </div>
    <?php endif; } ?>
  3. whooami
    Member
    Posted 6 years ago #

    you have to get the comment first I think:)

    $comment = get_comment($id);

    yeap that works too :P

Topic Closed

This topic has been closed to new replies.

About this Topic