• Resolved ThorHammer

    (@thorhammer)


    This should order posts by date:

    if( !function_exists('_sortByDate') ):
    function _sortByDate($a, $b)
    {
    return strtotime($b->post_date) - strtotime($a->post_date);
    }
    endif;
    usort($g_posts, '_sortByDate');

    But it doesn’t. Why?

    Second question:
    This is supposed to get a post’s commenters gravatar:

    <img src="<?php print $comment->author_avatar; ?>" alt="<?php print $comment->comment_author; ?>" />
    if( $the_comment->comment_author_email )
    {
    $the_comment->author_avatar = sprintf("https://www.gravatar.com/avatar/%s?s=68&d=mm&f=y", md5($cdata['comment']->comment_author_email));
    }
    else
    {
    $the_comment->author_avatar = "https://www.gravatar.com/avatar/00000000000000000000000000000000?s=68&d=mmf=y";
    }

    But it only returns the mysterious blank avatar. Why?

Viewing 8 replies - 1 through 8 (of 8 total)
  • 1. usort
    Try returning -1 or 1 based on your calculation

    return ( strtotime($b->post_date) - strtotime($a->post_date) ) ? -1 : 1;

    2. Gravatar
    The parameter f=y always enforces the default avatar. Leaving that out might already do the trick. Also Gravatar requires the string to be MD5 hashed being all-lowercase.

    md5( strtolower( $cdata['comment']->comment_author_email ) )

    Thread Starter ThorHammer

    (@thorhammer)

    1. worked, thank you!

    2. Still doesnt’t work. I changed the code like this:

    if( $the_comment->comment_author_email )
    {
    $the_comment->author_avatar = sprintf("https://www.gravatar.com/avatar/%s?s=68&d=mm", md5md5( strtolower($cdata['comment']->comment_author_email)));
    }
    else
    {
    $the_comment->author_avatar = "https://www.gravatar.com/avatar/00000000000000000000000000000000?s=68&d=mm";
    }

    And I show it like this:
    <img src="<?php print $comment->author_avatar; ?>" alt="<?php print $comment->comment_author; ?>" />

    The output in source is still the mysterious gravatar and looks like this:
    https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=68&d=mm

    While it should look like this:
    https://1.gravatar.com/avatar/7b5aa914b8010f629094a97a117861d6?s=96&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&r=G

    So I am still a bit lost regarding this one …

    i am also facing same problem

    Let’s go on a hunt ??

    1. Your MD5 hash seems to be d41d8cd98f00b204e9800998ecf8427e which is the case because $the_comment->comment_author_email seems to be null, ergo non-existing!
    md5( null ) = d41d8cd98f00b204e9800998ecf8427e.

    Could you check var_dump( $the_comment ); ?

    What theme are you using? Where is $the_comment defined?

    2. You if-clause is not required since Gravatar.com will automatically sent a/the default image in case the request is false.

    $commentAuthorEmail         = isset( $the_comment->comment_author_email ) ? $the_comment->comment_author_email : '';
    
    $avatar_default             = 'mm'; // 'mm' will automatically return the 'mystery man'. In this case no URL would be required to be specified!
    
    $avatar_width               = 68;
    
    $avatar_author              = 'https://www.gravatar.com/avatar/' . md5( strtolower( $commentAuthorEmail ) ) . '?s=' . (int) $avatar_width . '&d=' . urlencode( $avatar_default );
    
    $the_comment->author_avatar = $avatar_author;

    Thread Starter ThorHammer

    (@thorhammer)

    Thanks for your reply and investigation!
    Hm, I am using a child version of twenty twelve and this gravatars are meant to show up in widgets inside my two sidebars. The widget is custom built (not by me, but a professional) and it takes the last comments from two categories in a multisite install and cache the result and extracts data from the cache and displays it in the widget.
    It’s shown on the left and right sidebar here: (if you are lucky and get there before the cache is emptying itself and the sidebars are empty):
    https://www.litteraturbloggen.no

    Here is a bit more from the code:

    foreach($_posts as $blog => $posts)
    {
    $the_post =& $posts[0];
    if( is_array($the_post->comments) && count($the_post->comments) > 0 )
    {
    $the_comment =& $the_post->comments[0];
    if( $the_comment->comment_author_email )
    {
    $the_comment->author_avatar = sprintf("https://www.gravatar.com/avatar/%s?s=68&d=mm", md5($cdata['comment']->comment_author_email));
    }
    else

    …etc.

    How are $_posts and $cdata defined?

    There’s also a little inconsistancy (I guess): In the second IF clause your checking for $the_comment->comment_author_email but when requesting the avatar your using $cdata[‘comment’]->comment_author_email. Could you try
    $the_comment->author_avatar = sprintf("https://www.gravatar.com/avatar/%s?s=68&d=mm", md5( strtolower( $the_comment->comment_author_email ) ) );
    instead of
    $the_comment->author_avatar = sprintf("https://www.gravatar.com/avatar/%s?s=68&d=mm", md5($cdata['comment']->comment_author_email));

    Thread Starter ThorHammer

    (@thorhammer)

    YES!!!!

    My hero!!! You solved it!!

    Please accept a virtual beer from me ??

    Thanks, you’re welcome. Cheers! ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Two PHP-questions’ is closed to new replies.