
WordPress 评论插件
When we wrote about how to 显示 WordPress 中评论最多的帖子, we highlighted a plugin to make beginner’s life easier. However some of our users asked us if there was a way to display most commented posts without installing a plugin. In this article, we will share a code snippet that you can add to display most commented posts in WordPress without a plugin.
当我们撰写有关如何在 WordPress 中显示评论最多的帖子时 ,我们重点介绍了一个插件,可简化初学者的生活。 但是,我们的一些用户问我们是否有一种无需安装插件即可显示评论最多的帖子的方法。 在本文中,我们将共享一个代码片段,您可以添加该代码片段以显示WordPress中大多数无插件的帖子。
如果您正在学习构建 WordPress 主题并且不想使用插件,这非常有用。
如果您正在学习构建 WordPress 主题并且不想使用插件,这非常有用。
Please note, that this method is not suitable for beginners. If you do not feel confident about adding code, then you should checkout our guide on how to display WordPress 中评论最多的帖子 by using a plugin. If you are looking for a way to display your most popular content, then checkout our list of the best WordPress 的热门帖子插件.
请注意,此方法不适合初学者。 如果您对添加代码不满意,则应查阅我们的指南,以了解如何通过使用插件在 WordPress 中显示评论最多的帖子 。 如果您正在寻找一种显示最受欢迎的内容的方法,请查看最流行的 WordPress 帖子插件列表。
让我们开始吧,首先您需要将以下代码添加到您的主题或子主题的functions.php 文件或特定于站点的插件中。
让我们开始吧,首先您需要将以下代码添加到您的主题或子主题的functions.php 文件或特定于站点的插件中。
function wpb_most_commented_posts() {
// start output buffering
ob_start();
?>
<ul class="most-commented">
<?php
// Run WP_Query
// change posts_per_page value to limit the number of posts
$query = new WP_Query('orderby=comment_count&posts_per_page=10');
//begin loop
while ($query->have_posts()) : $query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <span class="wpb-comment-count"><?php comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></span></li>
<?php endwhile;
// end loop
?>
</ul>
<?php
// Turn off output buffering
$output = ob_get_clean();
//Return output
return $output;
}
// Create shortcode
add_shortcode('wpb_most_commented', 'wpb_most_commented_posts');
//Enable shortcode execution in text widgets
add_filter('widget_text', 'do_shortcode');
此代码运行数据库查询并按评论计数顺序获取 10 个帖子。我们使用了输出缓冲,以便我们可以使用代码来创建短代码。
此代码运行数据库查询并按评论计数的顺序获取 10 个帖子。我们已经使用了输出缓冲,因此我们可以使用代码创建一个短代码。
The last line enables shortcode execution in text widgets. Now in order to show the results, all you need to do is add [wpb_most_commented]
shortcode in a text widget, or in any WordPress post or page.
最后一行启用文本小部件中的短代码执行。 现在,为了显示结果,您需要做的就是在文本小部件或任何WordPress帖子或页面中添加[wpb_most_commented]
短代码。
To display post thumbnail next to post titles, you need to add this line just after <li>
and post title.
要在帖子标题旁边显示帖子缩略图,您需要在<li>
和帖子标题之后添加此行。
<?php the_post_thumbnail(array(40,40)); ?>
数组中使用的值将定义帖子缩略图的自定义大小。您可以调整它以满足您的需要。
数组中使用的值将定义帖子缩略图的自定义大小。您可以调整它以满足您的需要。
To style the output you can use .most-commented
and .wpb-comment-count
classes in your theme’s stylesheet. You can use this CSS to get started:
要设置输出样式,可以在主题的样式表中使用.wpb-comment-count
.most-commented
和.wpb-comment-count
类。 您可以使用以下CSS入门:
.most-commented li {
border-bottom:1px solid #eee;
padding-bottom:3px;
}
.most-commented li :after {
clear:both;
}
.most-commented img {
padding:3px;
margin:3px;
float:left;
}
.wpb_comment_count a, .wpb_comment_count a:active, .wpb_comment_count a:visited, .wpb_comment_count a:hover {
color:#FFF;
}
我们希望这篇文章可以帮助您在 WordPress 中显示最多评论的帖子,而无需安装新插件。请随意尝试代码和 CSS。
我们希望这篇文章可以帮助您在 WordPress 中显示评论最多的帖子,而无需安装新插件。请随意尝试代码和 CSS。
If you liked this article, then subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 谷歌+ or 推特.
如果您喜欢这篇文章,请订阅我们的YouTube 频道的WordPress视频教程。 您也可以在谷歌+或在推特上找到我们。
WordPress 评论插件