迭代式建主题(25):评论列表:wp_list_comments

作者: admin 分类: wordpress主题制作教程 发布时间: 2026-10-18 09:00

到这一轮为止,读者看完文章只能走人——没有评论区。这一篇先让评论显示出来,表单放到下一篇。

第一步:建 comments.php

<?php if ( post_password_required() ) { return; } ?>

<div id="comments">

    <?php if ( have_comments() ) : ?>
        <h2><?php comments_number( '没有评论', '1 条评论', '% 条评论' ); ?></h2>
        <ol class="comment-list">
            <?php wp_list_comments( array( 'style' => 'ol' ) ); ?>
        </ol>
    <?php endif; ?>

    <?php comment_form(); ?>

</div>

四块:密码保护时直接退出、有评论就显示数量、列出评论、放一个表单。

第二步:在 single.php 里调用

<?php comments_template(); ?>

放在正文后面、get_footer() 前面。这个函数会自动去找 comments.php,找不到就用 WordPress 自带的默认版。

几个函数的区别

  • have_comments():这篇有没有评论。
  • comments_number():按数量输出不同文案,三个参数分别是 0 条、1 条、多条时的写法,多条时用 % 代表数字。
  • wp_list_comments():把评论一条条列出来。
  • comment_form():输出评论表单。

现在评论能显示了。下一篇改表单的文案和字段。

本系列:迭代式建主题 · 目录(72 篇) | 上一篇 (24) | 下一篇 (26)

相关文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Powered by 草根站长笔记 © 2015-2026 草根站长笔记 Inc.版权所有,禁止转载