WordPress根据评论数获取热门文章的例子

作者:袖梨 2022-06-25

很多人使用WordPress中经常需要调用WordPress的热门文章功能,获取WordPress热门文章方式有很多,可以按文章的浏览量或者WordPress评论数,本文主要介绍按评论数来获取WordPress的热门文章。

以下将介绍如何通过文章的评论数来获取WordPress热门文章,代码如下:

热门文章



  • <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 6");

foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
{<?php echo $commentcount ?> 个评论}
<?php } } ?>

将以上一段代码添加到相应位置,比如添加到single.php文件中,就可以实现文章末尾显示热门文章了。代码中的LIMIT 0, 6为显示文章的数量,可以在此处修改需要的数量。

wordpress获取一周热门文章排行

<?php
function mostweek($where = '') {
//获取最近七天的文章
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
return $where;
}
add_filter('posts_where', 'mostweek'); ?>

<?php query_posts("v_sortby=views&caller_get_posts=1&orderby=date&v_orderby=desc&showposts=10") ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php endwhile; ?> <?php endif; ?> wordpress获取一月热门文章排行 <?php function mostmonth($where = '') { //获取最近30天文章 $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; return $where; } add_filter('posts_where', 'mostmonth'); ?> <?php query_posts("v_sortby=views&caller_get_posts=1&orderby=date&v_orderby=desc&showposts=10") ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php endwhile; ?> <?php endif; ?>

相关文章

精彩推荐