获取修改评论查询后的评论数量

时间:2015-09-11 作者:Shahrukh Khan

我已在中修改了注释查询参数comment-template.php 从…起

$author_id = get_query_var( \'author_id\', 1 );
        $comment_args = array(
                \'order\'   => \'ASC\',
                \'orderby\' => \'comment_date_gmt\',
                \'status\'  => \'approve\',
        );
    $comments = get_comments( $comment_args );
至以下代码

$author_id = get_query_var( \'author_id\', 1 );
    if($author_id == \'\') {
        $comment_args = array(
                \'order\'   => \'ASC\',
                \'orderby\' => \'comment_date_gmt\',
                \'status\'  => \'approve\',
        );
    }
    else { 
        $comment_args = array(
                \'order\'     => \'ASC\',
                \'orderby\'   => \'comment_date_gmt\',
                \'status\'    => \'approve\',
                \'meta_key\'  => \'custom_author_id\',
                \'meta_value\'=> $author_id,
            );
    }
    $comments = get_comments( $comment_args );
我想知道如何修改正在显示的评论数量,即。get_comments_number()

2 个回复
SO网友:bosco

避免编辑核心文件WordPress的开发考虑到了可扩展性,并提供了多种方法来更改安装的外观和功能,而无需更改WordPress软件本身(主要使用Hooks 结合PluginsThemes). 该软件包括;核心文件“;,通常是./wp-content 目录除外./wp-config.php, 和任何目录级服务器配置文件(.htaccess, 等等)。

编辑WordPress安装的核心文件通常既不必要也不可维护,因此stronglyyou run a much larger risk of introducing new security vulnerabilities into your WordPress installation.

而不是修改核心文件./wp-includes/comment-template.php, 一个更易于维护的解决方案是a comments.php template file 在自定义主题中(或a child theme 您当前的主题(如果是由第三方开发的)并使用WP_Comment_Query 在其中指定新参数。使用numberoffset 用于指定所需注释范围的参数。

Template File ./wp-content/themes/wpse202457_theme/comments.php:

<?php
  $author_id = get_query_var( \'author_id\', \'\' );
  $comments = array();

  if( $author_id != \'\' ) {
    $comment_args = array(
      \'order\'      => \'ASC\',
      \'orderby\'    => \'comment_date_gmt\',
      \'status\'     => \'approve\',
      \'meta_key\'   => \'custom_author_id\',
      \'meta_value\' => $author_id,
      \'number\'     => 20, // Retrieve 20 comments...
      \'offset\'     => 40  // ...starting from the 40th comment.
    );
    
    $comment_query = new WP_Comment_Query;
    $comments = $comment_query->query( $comment_args );
  }
  
  
  if( ! empty( $comments ) ) {
    // The Comments Loop
    foreach( $comments as $comment ) {
      echo( \'<p>\' . $comment->comment_content . \'</p><br/><br/>\' );
    }
  }
  else {
    echo( \'<p>No comments found</p>\' );
  }
?>
退房this excellent question 有关如何使用numberoffset 动态参数。挖掘其他主题\'comments.php 一些示例标记的模板文件。

您可以将替换注释模板放置在插件中,并使用comments_template filter 要将注释模板包含重定向到它,请执行以下操作:

Plugin File ./wp-content/plugins/wpse202457_comments_plugin/wpse202457CommentsPlugin.php:

function wpse202457_comments_template( $comment_template ) {
  // Redirect ALL comment template inclusions to "./wp-content/plugins/wpse202457_comments_plugin/comments.php"
  return dirname(__FILE__) . \'/comments.php\';
}

add_filter( \'comments_template\', \'wpse202457_comments_template\' );

附录

针对@Rarst建议的评论get_comments_query() 函数将仅返回为主查询中显示的post对象记录的注释数。

要获取更改查询的注释数,只需count() 查询结果,即:

$comment_query = new WP_Comment_Query;
$comments = $comment_query->query( $comment_args );
$comment_count = count( $comments );
However, 如果您正在使用number 参数指定要检索的注释数,然后$comment_count 将始终等于或小于number. 要获取与参数匹配的注释总数,您需要省略number 参数和count() 结果,或设置count 参数到true, 在这种情况下,查询将返回匹配注释的数量,而不返回注释本身:

$author_id = get_query_var( \'author_id\', 1 );
$comment_args = array(
  \'status\'     => \'approve\',
  \'meta_key\'   => \'custom_author_id\',
  \'meta_value\' => $author_id,
  \'count\'      => true // Return the NUMBER of matching comments, not the matching comment objects themselves
);

$comment_query = new WP_Comment_Query;
$comment_count = $comment_query->query( $comment_args );
如果您不打算对WP_Comment_Query 对象,您可能希望创建一些方便的函数,并将它们放在插件或自定义主题的functions.php 用于快速访问。大致如下:

// Get comments with a custom_author_id meta-value of $author_id
function wpse202471_get_author_comments( $author_id = null, $args = array() ) {
  // If an author id was not passed, see if there\'s one in the query vars
  if( ! isset( $author_id ) )
    $author_id = get_query_var( \'author_id\', null );

  // If no author id was provided, return a negative result
  if( ! isset( $author_id ) ) {
    if( isset( $args[ \'count\' ] ) && $args[ \'count\' ] )
      return 0;

    return array();
  }

  // Merge query argument arrays
  $comment_args = array_merge(
    array(    // Default values
      \'order\'      => \'ASC\',
      \'orderby\'    => \'comment_date_gmt\',
      \'status\'     => \'approve\'
    ),
    $args,    // Any arguments passed to the function
    array(    // Merge meta-arguments last, so nothing can overwrite them
      \'meta_key\'   => \'custom_author_id\',
      \'meta_value\' => $author_id
    )
  );

  // Perform the query and return the results
  $comment_query = new WP_Comment_Query;
  return $comment_query->query( $comment_args );
}

// Get the total number of comments with a custom_author_id meta-value of $author_id
function wpse202471_get_author_comment_count( $author_id = null, $args = array() ) {
  $args[ \'count\' ] = true;
  
  // Remove query arguments that would prevent the proper count from being returned
  if( isset( $args[ \'number\' ] ) )
    unset( $args[ \'number\' ] );

  if( isset( $args[ \'offset\' ] ) )
    unset( $args[ \'offset\' ] );

  return wpse202471_get_author_comments( $author_id, $args );
}

SO网友:Rarst

get_comments_number() 不依赖于任何注释查询,它只报告持久存储在post对象中的注释数(comment_count 属性)。

您在示例中省略了查询本身的确切外观。如果您使用的是实际WP_Comment_Query 对象然后其comments 属性将具有您可以count(). 如果使用API函数可能会更加混乱。