Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
comments_template() - 加载在$file中指定的评论模板 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / comments_template()

comments_template( string $file = '/comments.php', bool $separate_comments = false )

加载在$file中指定的评论模板


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 2 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "8a120dc4a793b62501729a9a51316687" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "1a4541a803a48108dcb6f8964b22d929" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

comment 评论more...

templatemore...


描述

如果不是在文章单页或页面上,或者文章没有评论,则不会显示评论模板。

使用WordPress数据库对象查询评论。评论通过‘comments_array’过滤器钩子传递,分别带有评论列表和文章ID。

$file路径通过一个名为‘comments_template’的过滤器钩子传递,其中包括TEMPLATEPATH和$file组合。首先尝试$filtered路径,如果失败,则需要来自默认主题的默认评论模板。如果其中一个不存在,则WordPress进程将暂停。因此,建议不要删除默认主题。

如果文章没有评论,则不会尝试去获取。


参数

$file

(string) (可选) 要加载的文件。

默认值: '/comments.php'

$separate_comments

(bool) (可选) 是否按评论类型分隔评论。

默认值: false



源码

查看源码 官方文档


更新日志

版本描述
1.5.0开始引入

使用示例

  • 示例1

    在某些情况下,您可能希望在主题中以不同的方式显示您的评论。为此,您将构建一个备用文件(例如short-comments.php),并按如下方式调用它:

    <?php comments_template( '/short-comments.php' ); ?> 
    

    用于替代评论模板的文件路径应相对于当前主题根目录,并包括任意子文件夹。因此,如果自定义评论模板位于主题内的文件夹中,则调用时可能如下所示:

    <?php comments_template( '/custom-templates/alternative-comments.php' ); ?> 
    
    
  • 示例2

    在page.php和single.php中使用

    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :
    	comments_template();
    endif;
    
  • 示例3

    默认用法

    <?php comments_template(); ?>