WordPress网站制作热门文章排行榜(浏览量排序)
更新时间:2025-09-13 12:15:34|栏目:PHP教程|点击:19 次
使用Wordpress做网站,有时需要在网站的侧边栏调用热门文章,我们经常是按评论数进行排序的,今天介绍一下如何调用以浏览量排序的热门文章的方法。
方法/步骤:
1、先要在自己的WORDPRESS网站模板函数文件functions.php中添加以下的浏览量函数代码,用于调用文章浏览量。代码见:
wordpress显示文章浏览量次数[文章]
2、然后在需要调用按浏览量排序的热门文章位置,使用以下的代码进行调用文章列表;
<ul> <?php $args=array( 'meta_key' => 'views', 'orderby' => 'meta_value_num', 'posts_per_page'=>6, 'order' => 'DESC' ); query_posts($args); while (have_posts()) : the_post();?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li> <?php endwhile;wp_reset_query();?> </ul> |
3、这样就可以调用出用户浏览最多的6篇文章了。
更新:如果想对显示的文章列表做控制,可以使用以下的代码:
1、排除置顶文章
<ul> <?php $args=array( 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page'=>6, 'order' => 'DESC' ); query_posts($args); while (have_posts()) : the_post();?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li> <?php endwhile;wp_reset_query();?> </ul> |
2、排除指定分类下的文章
<ul> <?php $args=array( 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'post__not_in' => get_option( 'sticky_posts' ), 'category__not_in' => array('1','2'), 'posts_per_page'=>6, 'order' => 'DESC' ); query_posts($args); while (have_posts()) : the_post();?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li> <?php endwhile;wp_reset_query();?> </ul> |
3、调用指定分类下的热门文章
<ul> <?php $args=array( 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'post__not_in' => get_option( 'sticky_posts' ), 'cat' => array('1','2'), 'posts_per_page'=>6, 'order' => 'DESC' ); query_posts($args); while (have_posts()) : the_post();?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li> <?php endwhile;wp_reset_query();?> </ul> |
4、提取30天时间段浏览量最多的文章。
<?php $date_query=array( array( 'column' => 'post_date', 'before' => date('Y-m-d',time()+3600*24), 'after' =>date('Y-m-d',time()-3600*24*30) //此处30标示三十天内浏览量 ) ); ?> <?php $args = array( 'posts_per_page' =>1, 'cat' =>26, 'post__in' => get_option( 'sticky_posts' ), 'ignore_sticky_posts' => 1, 'meta_key' => 'views', 'date_query' => $date_query, 'orderby'=> 'meta_value_num', ); query_posts( $args ); while ( have_posts() ) : the_post();?> <h4><b><a href="<?php the_permalink(); ?>"><?php echo mb_strimwidth(get_the_title(), 0,100, ''); ?></a> </b></h4> <?php endwhile;wp_reset_query();?> |
栏 目:PHP教程
下一篇:PHP 7安装使用体验之性能大提升,兼容性强,扩展支
本文标题:PHP中实现九九乘法口诀表
本文地址:http://tp2.com/index.php?m=home&c=View&a=index&aid=172
您可能感兴趣的文章
- 11-23PHP面向对象 封装与继承
- 11-23PHP 7安装使用体验之性能大提升,兼容性强,扩展支
- 11-23PHP中实现九九乘法口诀表
- 11-23PHP中实现一个简易三角形的方法
- 11-23PHP中利用for循环判断一个数是不是回文数
- 11-23隐藏PHP版本与PHP基本安全设置
- 11-23nginx+php 打开php错误提示的方法
- 11-23php中 == 和 === 的区别和应用说明
- 11-23PHP常用正则表达式汇总大全
- 11-23PHP保留两位小数的几种方法


阅读排行
推荐教程
- 11-22PHP自定义函数判断是否为Get、Post及Ajax提交的方法
- 11-23php中 == 和 === 的区别和应用说明
- 11-23PHP 7安装使用体验之性能大提升,兼容性强,扩展支
- 11-23隐藏PHP版本与PHP基本安全设置
- 11-23PHP+MYSQL 读写分离简单实战
- 11-22详解PHP防止直接访问.php 文件的实现方法
- 11-22PHP编程求最大公约数与最小公倍数的方法示例
- 11-22PHP实现上传多图即时显示与即时删除的方法
- 11-23PHP保留两位小数的几种方法
- 11-23nginx+php 打开php错误提示的方法