Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How would I achieve this condition ...

    if lastest comment is < 7 days old 
        echo 'New Comment';
    else ''
share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

To get the latest comment use get_comments().
get_comment_date returns the date of a comment in any format for PHP's date().

Now it is easy. Let's put the logic into a function to keep the global namespace clean:

/**
 * Returns the number of days since the latest comment.
 *
 * @return int
 */
function get_days_since_last_comment( $post_id = 0 )
{
    $args = array (
            'number' => 1,
            'status' => 'approve'
        );
    0 !== $post_id and $args['post_id'] = (int) $post_id;
    // Array of comment objects.
    $latest_comment = get_comments( $args );

    // No comments found.
    if ( ! $latest_comment )
    {
        return -1;
    }

    $comment_unix = get_comment_date( 'U', $latest_comment[0]->comment_ID );
    return round( ( time() - $comment_unix ) / 86400 );
}

Add the function to your plugin or to your theme's functions.php.

To display a special message:

if ( get_days_since_last_comment() > 7 )
{
    print 'Looks like everything has been said.';
}

To get the days for a specific post (here ID 123) :

if ( get_days_since_last_comment( 123 ) > 7 )
{
    print 'Looks like everything has been said.';
}
share|improve this answer
    
Thanks for that speedy reply @toscho. Will the function work in the loop (index)? –  TheLoneCuber Apr 30 '12 at 3:39
    
When a global post object exists it will work. You may alter the function to accept a post ID as a parameter to pass it to get_comments(). –  toscho Apr 30 '12 at 4:04
    
Thanks @tosho. If anyone can example "to accept a post ID as a parameter to pass it to get_comments()" I'm all ears. –  TheLoneCuber May 1 '12 at 23:05
    
See my update. :) –  toscho May 1 '12 at 23:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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