GET_COMMENTS_NUMBER of Depth-1(级别1)(1帖子)

时间:2013-01-07 作者:Mathieu

我只为我的wordpress网站选择了两个级别的评论。如何显示第一级的注释数?(深度-1)

4 个回复
SO网友:akTed

在wp\\U注释表中,如果comment_parent 等于0,这是一级注释。

function get_num_toplevel_comments() {
    global $wpdb;

    return $wpdb->get_var("
        SELECT COUNT(*) 
        FROM $wpdb->comments
        WHERE comment_parent = 0
    ");
}

SO网友:GDY

基于akTed\'s答案:

function get_top_level_comments_number( $post_id = 0, $onlyapproved = true ) {

    global $wpdb, $post;

    $post_id = $post_id ? $post_id : $post->ID;

    $sql = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_parent = 0 AND comment_post_ID = $post_id";

    if( $onlyapproved ) $sql .= " AND comment_approved=\'1\'";

    return (int) $wpdb->get_var( $sql );

}
用法与相同here … 返回特定帖子的顶级评论数(如果未提供id,则返回当前评论数)

SO网友:fuxia

使用中的代码this answer 只删除一个!:

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

$comments = get_comments();

remove_filter( \'comments_clauses\', \'wpse_78628_top_comments_only\' );

function wpse_78628_top_comments_only( $clauses )
{
    $clauses[\'where\'] .= \' AND comment_parent = 0\';
    return $clauses;
}

SO网友:user3003106

下面是一些完整的工作代码,基于前面的答案。

  <?php
add_filter( \'comments_clauses\', \'wpse_78490_child_comments_only\' );

function wpse_78490_child_comments_only( $clauses )
{
    $clauses[\'where\'] .= \' AND comment_parent != 0\';
    return $clauses;
}
  $count = get_comments( array(\'post_id\' => $post->ID, \'count\' => true) ); echo $count; ?>

结束