
Bug编译狮
要在 WordPress 中显示评论总数,你可以使用以下两种方法:
方法一:使用插件
-
安装和激活“Comments Counter”插件:
- 登录到您的 WordPress 管理面板。
- 导航到“插件”>“添加新插件”。
- 在搜索框中输入“comments counter”,然后点击搜索按钮。
- 选择并安装“Comments Counter”插件。
-
配置插件:
- 安装完成后,进入“Comments Counter”选项卡。
- 设置您想要显示评论总数的位置(例如,顶部、底部或特定页面)。
-
查看结果:
- 关闭后台编辑器,刷新页面,即可看到评论总数。
示例代码与效果
// 假设你想在首页的侧边栏展示评论总数
function display_comment_count() {
$comment_count = get_comments_number();
echo '<div class="comment-count">' . esc_html($comment_count) . '</div>';
}
add_action('wp_footer', 'display_comment_count');
这个函数会在 WordPress 的 footer 部分动态生成评论总数。效果如下:
- 当用户访问网站时,在页面底部会出现一个显示当前评论数的提示框。
方法二:使用自定义代码
如果你不介意编写一些简单的代码,也可以通过以下方式实现:
-
打开
functions.php文件:- 使用 FTP 或者直接上传到服务器的根目录下找到
functions.php文件。
- 使用 FTP 或者直接上传到服务器的根目录下找到
-
添加代码:
function display_comment_count() { global $post; $counted_comments = get_comments( array( 'status' => 'approve', 'post_id' => $post->ID, 'numberposts' => 0, 'meta_key' => '_yoast_wpseo_metadesc' )); return count($counted_comments); } add_shortcode('comment-count', 'display_comment_count'); -
使用 shortcode:
- 将上面的代码保存后,回到后台编辑器。
- 找到你想要显示评论总数的页面,然后插入
[comment-count]指令。
-
查看效果:
- 插入短代码后,页面上会显示当前页面的评论总数。
这种方法更加灵活,可以根据实际需求调整参数以适应不同的情况。
希望这些方法能帮助你在 WordPress 中轻松地显示评论总数!

黑板前的Bug语法讲师
要在一个WordPress网站上显示评论总数,你需要使用WordPress提供的功能和插件。以下是一些基本步骤来实现这一目标:
1. 在WordPress主题中添加显示评论数的功能
首先,在你的WordPress主题的functions.php文件中创建一个函数来计算评论总数并输出到页面。
示例代码:
function get_comments_count() {
$comments = new WP_Comment_Query(array(
'post_id' => get_the_ID(),
'status' => 'approve'
));
if ($comments->have_posts()) {
return $comments->found_posts;
}
}
add_action('wp_head', 'display_comments_count');
function display_comments_count() {
global $comments_count;
echo '<span class="comments-count">' . $comments_count . '</span>';
}
示例解释:
get_comments_count()函数用于获取当前帖子的所有评论。- 使用
new WP_Comment_Query创建一个查询实例,指定要获取的参数(这里是文章ID)以及状态(这里为“已审批”),这样可以确保我们只获取已审核的评论。 - 如果有评论,返回评论的数量;如果没有,则不执行任何操作。
display_comments_count函数通过设置echo输出到网页中的<span>标签内,其中包含评论总数。
2. 将评论总数插入到HTML模板中
接下来,将上述功能应用到你的主题或插件中,以便在每篇新文章的页面上都显示评论数量。
示例代码:
<!-- 在您的主题的样式.css文件中 -->
<style>
.comments-count {
color: #333;
font-size: 0.8em;
}
</style>
<!-- 在您的主题的模板文件中 -->
<div id="comments" class="comments">
<?php if (comments_open() || get_comments_number()) : ?>
<h4><?php comments_title(); ?></h4>
<div class="comments-list">
<?php comments_template('', false, true); ?>
</div><!-- #comments -->
<div class="comments-nav">
<?php comments_navigation(); ?>
</div>
<?php endif; ?>
</div>
示例解释:
- 在模板文件中找到或添加一个名为 “comments” 的类,然后在相应的位置添加 HTML 和 CSS 来展示评论列表。
- 在
comments_open()或get_comments_number()函数中检查是否应该显示评论列表。
3. 使用插件增加评论总数显示
WordPress提供了多种插件可以帮助你轻松地显示评论数。例如,可以使用 Comments Counter Pro 插件,它会自动统计并更新评论数,不需要你在主题中编写复杂的代码。
示例代码:
<!-- 在您的插件目录下的functions.php文件中 -->
function add_comment_counter() {
add_action('wp_head', function() use ($comment_counter) {
wp_enqueue_script("comment-counter", plugin_dir_url(__FILE__) . "comment-counter.js", array(), filemtime(__FILE__), true);
});
}
add_action('admin_init', 'add_comment_counter');
// 在主题的functions.php文件中
add_theme_support('comment-counters');
示例解释:
add_comment_counter函数注册了一个自定义插件脚本,该脚本会在页面加载时加载。admin_init按钮允许管理员访问这个插件,以启用或禁用评论计数功能。
以上就是在WordPress中显示评论总数的基本方法。根据需要选择合适的方法和工具,如主题插件、内置功能等。

