要获取最新评论,请使用get_comments()
.
get_comment_date
返回任何格式的注释的日期,以供PHP使用date()
.
现在很容易了。让我们将逻辑放入一个函数中,以保持全局命名空间干净:
/**
* 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 );
}
将函数添加到插件或主题
functions.php
.
要显示特殊消息,请执行以下操作:
if ( get_days_since_last_comment() > 7 )
{
print \'Looks like everything has been said.\';
}
获取特定帖子的天数(此处ID
123
) :
if ( get_days_since_last_comment( 123 ) > 7 )
{
print \'Looks like everything has been said.\';
}