如何输出每天一篇帖子的评论数?

时间:2013-05-13 作者:user1627363

我有一个关于帖子中评论数字的问题。。。现在,我可以在循环外的帖子中输出评论总数。这很酷,但我想show the comment number/amount of a post PER DAY. 所以让我们假设我有一篇帖子,人们今天评论了10次。。。然后,我想在帖子标题后显示“10条评论”。。。如果一天过去了,第二天只有一个人发表评论,我希望它在帖子标题后输出“1条评论”。。。

这是否可能,如果可能,如何实现?

我想使用以下函数输出“每篇文章之后的每篇文章今天的评论”:

function wowPosts($num) {
    global $wpdb;

    $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");

    foreach ($posts as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $title = $post->post_title;
        $count = $post->comment_count;
        $comment_count = get_comment_count($post->ID);
        $all_comments = get_comment_count( array ( \'post_id\' => get_the_ID() ) );

        if ($count != 0) {
            $popular .= \'<li>\';
            $popular .= \'<a href="\' . get_permalink($id) . \'" title="\' . $title . \'">\' . $title . \'</a> \'. count( $all_comments ) . \' \';
            $popular .= \'</li>\';
        }
    }
    return $popular;
}

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

没有参数可以将注释查询直接限制到给定日期。以后必须过滤查询:

/**
 * Get the number of comments for a post today.
 *
 * @param  int $post_id
 * @return int
 */
function t5_count_comments_today( $post_id = NULL )
{
    if ( NULL === $post_id )
        $post_id = get_the_ID();

    add_filter( \'comments_clauses\', \'t5_comments_today_sql\' );

    // get_comments() returns a string even with \'count\' set to TRUE.
    return (int) get_comments(
        array (
            \'post_id\' => $post_id,
            \'count\'   => TRUE
        )
    );
}

/**
 * Add date specific WHERE clause to comment query.
 *
 * Deactivates itself to avoid collisions with regular comment queries.
 *
 * @wp-hook comments_clauses
 * @param   array $clauses
 * @return  array
 */
function t5_comments_today_sql( $clauses )
{
    // avoid collisions
    remove_filter( current_filter(), __FUNCTION__ );

    global $wpdb;
    $clauses[\'where\'] .= $wpdb->prepare( \' AND comment_date >= %s\', date( \'Y-m-d 00:00:00\' ) );
    return $clauses;
}

简单用法

echo t5_count_comments_today();
或者,如果您不想显示零评论:

$comments_today = t5_count_comments_today();
if ( 0 < $comments_today )
    echo $comments_today;
扩展用例将数字添加到comments_popup_link().

is_admin() || add_filter( \'comments_number\', \'t5_comment_number_today\', 10, 2 );

/**
 * Add formatted number to total comments number.
 *
 * @wp-hook comments_number
 * @param   string $output
 * @param   int    $number Might be a string, so we cast it to int.
 * @return  string
 */
function t5_comment_number_today( $output, $number )
{
    // no comments at all, no need to count today\'s comments
    if ( 0 === (int) $number )
        return $output;

    $count = t5_count_comments_today();
    $add   = \'no comments today\'; // default text

    if ( 1 === $count )
        $add = \'one comment today\';

    if ( 1 < $count )
        $add = "$count comments today";

    return "$output ($add)";
}
旁注:始终使用get_comments() 或班级WP_Comment_Query 为此get_comments() 是一个包装器,因为结果将被缓存,所以您永远不必为相同的参数列表两次获取相同的值
在这些情况下,请避免使用原始SQL进行非API调用。

SO网友:Pat J

WordPress用于存储注释的数据库表中有几个日期字段--comment_datecomment_date_gmt. 您应该能够执行以下操作:

function wpse99287_comment_count( $content ) {
    global $wpdb;
    global $post;
    $today = date( \'Y-m-d 00:00:00\' );
    $tomorrow = date( \'Y-m-d 23:59:59\' );
    $sql = "SELECT COUNT(comment_id)
            FROM {$wpdb->comments} 
            WHERE comment_post_ID = {$post->ID}
            AND comment_approved = 1
            AND comment_date >= \'$today\'
            AND comment_date <= \'$tomorrow\'";
    $comment_count = $wpdb->get_var( $sql );
    $content = "<div class=\'comment-count\'>$comment_count comment(s) today</div>" . $content;
}
add_filter( \'the_content\', \'wpse99287_comment_count\' );
这将在<div> 在帖子内容的开头。

参考文献

wpdb class

结束

相关推荐

Number of displayed posts

我已经尝试了很多方法来解决这个问题。所以我有一个插件,它在一个小部件中显示分配给用户的项目。它将项目显示为帖子。问题是它只显示5 页面上的项目。我正在发送一个显示帖子的代码:$projects = get_posts(array(\'post_type\' => \'projects\')); 有个人叫我把它改成$projects = get_posts(array(\'post_type\' => \'projects\', \'number posts\' => 10));