• Resolved Jeremy Clark

    (@jeremyclark13)


    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jeremy Clark

    (@jeremyclark13)

    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; } ?>

    you have to get the comment first I think:)

    $comment = get_comment($id);

    yeap that works too 😛

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘A little theme function help (get_avatar)’ is closed to new replies.