避免编辑核心文件WordPress的开发考虑到了可扩展性,并提供了多种方法来更改安装的外观和功能,而无需更改WordPress软件本身(主要使用Hooks 结合Plugins 和Themes). 该软件包括;核心文件“;,通常是./wp-content
目录除外./wp-config.php
, 和任何目录级服务器配置文件(.htaccess
, 等等)。
编辑WordPress安装的核心文件通常既不必要也不可维护,因此strongly除非你找不到其他方法来达到你的目的,并且熟悉这样做的后果,否则你会感到沮丧;其中包括自动更新覆盖您的更改,WordPress社区基本上不支持您的更改,也许最重要的是,you 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 在其中指定新参数。使用number
和offset
用于指定所需注释范围的参数。
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 有关如何使用
number
和
offset
动态参数。挖掘其他主题\'
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 );
}