无需插件在WordPress侧边栏中显示热门内容

有一些插件可以实现这个功能,但如果你不想用插件的话,可以考虑下面的方法。

很简单,把以下代码粘贴到sidebar.php里去:

<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5″);
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href=”<?php echo get_permalink($postid); ?>” title=”<?php echo $title ?>”>
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>

大功告成!

原文:How to: Display your most popular content in your blog sidebar

原创文章,作者:网贝WebBay,如若转载,请注明出处:https://www.webbay.cn/how-to-display-your-most-popular-content-in-your-blog-sidebar

发表回复

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

error: Content is protected !!