WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Two PHP-questions (9 posts)

  1. ThorHammer
    Member
    Posted 1 year ago #

    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("http://www.gravatar.com/avatar/%s?s=68&d=mm&f=y", md5($cdata['comment']->comment_author_email));
    }
    else
    {
    $the_comment->author_avatar = "http://www.gravatar.com/avatar/00000000000000000000000000000000?s=68&d=mmf=y";
    }

    But it only returns the mysterious blank avatar. Why?

  2. Chris
    Member
    Posted 1 year ago #

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

  3. ThorHammer
    Member
    Posted 1 year ago #

    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("http://www.gravatar.com/avatar/%s?s=68&d=mm", md5md5( strtolower($cdata['comment']->comment_author_email)));
    }
    else
    {
    $the_comment->author_avatar = "http://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:
    http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=68&d=mm

    While it should look like this:
    http://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 ...

  4. jeffmathew
    Member
    Posted 1 year ago #

    i am also facing same problem

  5. Chris
    Member
    Posted 1 year ago #

    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              = 'http://www.gravatar.com/avatar/' . md5( strtolower( $commentAuthorEmail ) ) . '?s=' . (int) $avatar_width . '&d=' . urlencode( $avatar_default );
    
    $the_comment->author_avatar = $avatar_author;
  6. ThorHammer
    Member
    Posted 1 year ago #

    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):
    http://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("http://www.gravatar.com/avatar/%s?s=68&d=mm", md5($cdata['comment']->comment_author_email));
    }
    else
    ...etc.

  7. Chris
    Member
    Posted 1 year ago #

    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("http://www.gravatar.com/avatar/%s?s=68&d=mm", md5( strtolower( $the_comment->comment_author_email ) ) );
    instead of
    $the_comment->author_avatar = sprintf("http://www.gravatar.com/avatar/%s?s=68&d=mm", md5($cdata['comment']->comment_author_email));

  8. ThorHammer
    Member
    Posted 1 year ago #

    YES!!!!

    My hero!!! You solved it!!

    Please accept a virtual beer from me :-D

  9. Chris
    Member
    Posted 1 year ago #

    Thanks, you're welcome. Cheers! :)

Topic Closed

This topic has been closed to new replies.

About this Topic