完美实现wordpress主题文章阅读浏览量排行功能


实现一个wordpress文章阅读排名的功能,可以通过以下代码实现,但是需要一个前提就是你的当前wordpress主题需要集成或是安装了WP-PostViews文章浏览数据统计插件,才能够综合应用到我们接下来的代码中,实现我们的阅读量排行功能,下面测速网将以30天内的点击文章浏览量排名为基础分享给大家以下的操作步骤,大家可以举一反三,作出周排名、月排名甚至24小时排名。

一、找到当前主题中的functions.php文件,然后将以睛代码复制进该文件中:

12345678910111213141516171819202122232425262728//本文代码为30天内点击排行榜 //请将此代码放在functions.php文件的<?php和?>之间的位置 //编码时请选择专业的编码工具 function?mostViewedPostList($mode?=?”,?$limit?=?10,?$chars?=?0,?$display?=?true)?{????global?$wpdb,?$post;????$views_options?=?get_option(‘views_options’);????$where?=?”; ????$temp?=?”;????$output?=?”; ????if(!emptyempty($mode)?&&?$mode?!=?‘both’)?{????????$where?=?“post_type?=?‘$mode'”;????}?else?{ ????????$where?=?‘1=1′; ????} ????$mostViewedPostList?=?$wpdb->get_results(“SELECT?DISTINCT?$wpdb->posts.*,?(meta_value+0)?AS?views?FROM?$wpdb->posts?LEFT?JOIN?$wpdb->postmeta?ON?$wpdb->postmeta.post_id?=?$wpdb->posts.ID?WHERE?post_date?>?‘”?.?date(‘Y-m-d’,?strtotime(‘-30?days’))?.?“‘?AND?$where?AND?post_status?=?‘publish’?AND?meta_key?=?‘views’?AND?post_password?=?”?ORDER??BY?views?DESC?LIMIT?$limit”);????if($mostViewedPostList)?{????????foreach?($mostViewedPostList?as?$post)?{????????????$post_views?=?intval($post->views);????????????$post_title?=?get_the_title(); ????????????if($chars?>?0)?{????????????????$post_title?=?snippet_text($post_title,?$chars);????????????} ????????????$post_excerpt?=?views_post_excerpt($post->post_excerpt,?$post->post_content,?$post->post_password,?$chars);????????????$post_content?=?get_the_content();????????????$temp?=?stripslashes($views_options['mostViewedPostList_template']);????????????$temp?=?str_replace(“%VIEW_COUNT%”,?number_format_i18n($post_views),?$temp);????????????$temp?=?str_replace(“%POST_TITLE%”,?$post_title,?$temp);????????????$temp?=?str_replace(“%POST_EXCERPT%”,?$post_excerpt,?$temp);????????????$temp?=?str_replace(“%POST_CONTENT%”,?$post_content,?$temp);????????????$temp?=?str_replace(“%POST_URL%”,?get_permalink(),?$temp);????????????$output?.=?$temp;????????} ????}?else?{ ????????$output?=?‘<li>N/A</li>’.“\n”;????} ????if($display)?{????????echo?$output; ????}?else?{ ????????return?$output;????} }

找到我们需要显示排名的位置或页面,将下面代码复制到页面内。

12345678<?php if?(function_exists(‘get_mostViewedPostList’)?&?function_exists(‘mostViewedPostList’)){????echo?‘<ul>'; //10为调用的篇数,根据自己实际情况修改 ????mostViewedPostList(‘post’,10); ????echo?‘</ul>'; } ?>

上一篇:利用百度云规则让你的wordpress加速到秒开

下一篇:wordpress更新插件网站后台无法正常打开


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

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