创建按点击阅读次数排列的wordpress热门文章列表


很多的wordpress主题站点都是hot列表或是热门文章分类,怎样实现这个功能呢,其实很简单,只需要两组代码就可以实现。
首先我们要确保主题内有显示文章次数的代码,然后再对代码进行二次调用,怎样实现wordpress文章点击数次功能呢?将下面的代码复制到主题的functions.php文件内,可以实现主题的阅读量显示功能。

16171819202122/*实现文章浏览次数*/function getPostViews($postID) {$count_key = "post_views_count";$count = get_post_meta ( $postID, $count_key, true );if ($count == "") {delete_post_meta ( $postID, $count_key );add_post_meta ( $postID, $count_key, "0" );return "0 View";}return $count . " Views";} function setPostViews($postID) {$count_key = "post_views_count";$count = get_post_meta ( $postID, $count_key, true );if ($count == "") {$count = 0;delete_post_meta ( $postID, $count_key );add_post_meta ( $postID, $count_key, "0" );} else {$count ++;update_post_meta ( $postID, $count_key, $count );}}

通过上面的代码增加功能之后,需要我们再运用下面的代码进行功能调用,将如下代码复制插入到想要显示浏览次数的位置上
这里是文章内容页面使用的。

12<?php setPostViews(get_the_ID()); ?><?php echo getPostViews(get_the_ID()); ?>

这个是页面中使用的。

1<?php echo getPostViews(get_the_ID()); ?>

然后进入到我们最后的环节在需要调用文章的地方插入以下代码即可实现按点击数量排序的热门文章列表

12345678910111213<?php$post_num = 5; // 设置调用条数$args = array("meta_key" => "post_views_count","orderby" => "meta_value_num","order" => "DESC","posts_per_page" => $post_num);$query_posts = new WP_Query();$query_posts->query($args);while( $query_posts->have_posts() ) { $query_posts->the_post(); ?><h2><a href="<?php%20the_permalink();%20?>" title="<?php the_title_attribute(); ?>"><?php the_title();?></a></h2><?php } wp_reset_query();?>


上一篇:利用百度平台实现wordpress站外链接完美跳转页

下一篇:获取wordpress文章中所有的图片及图片数量


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器