WordPress 如何用子主题来修改评论模板comments.php

最近有人找模板兔二次开发modown主题,需要修改评论显示,要不影响主题升级的情况下修改,那么需要用到子主题。

怎样才能用子主题来覆盖默认的评论模板函数comments_template引用的模板文件呢?

将以下代码加到子主题的functions.php里,然后子主题里创建一个comments-new.php文件即可。

add_filter( "comments_template", "modown_child_comment_template" );
function modown_child_comment_template( $comment_template ) {
global $post;
if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
return;
}
return dirname(__FILE__) ."/comments-new.php";
}