调用评论模板comments_template()

讲真对函数一无所知,这篇纯粹作为备忘录存着,有时间慢慢消化。具体来源于"博客吧"《wordpress评论模板调用函数comments_template()》,wordpress函数comments_template()的作用是加载评论模板,用于wordpress文章内容页面(包括自定义文章类型)或单页面,是wordpress模板制作中一个非常重要的函数。

函数代码:

<?php comments_template($file, $separate_comments);?>

参数说明:
  • $file – 字符串string,要加载的文件,可选,默认调用comments.php文件,路径/comments.php,位于主题根目录;
  • $separate_comments – 布尔值boolean,是否根据评论的类型划分评论,可选,默认false(否)
示例:
  • 由于两个参数都是可选参数,可以直接使用,调用默认评论模板文件comments.php,代码如下:
    <?php comments_template();?>
  • 调用不同的评论模板文件,evaluate-comments.php文件保存在主题根目录,如果有两套评论显示样式就这样使用:
    <?php comments_template('/evaluate-comments.php');?>
  • 根据不同的文章类型显示文章评论:
    <?php comments_template(get_post_format().'-comment.php', true); ?>
函数位置:

comments_template()位于 wp-includes/comment-template.php

另:

在如下语句
<?php endwhile; endif; ?>
之前,或之后加上下面这句,便可以调用评论模板 :
<?php if ( comments_open() ) comments_template(); ?>