替换HotNews Pro主题热门文章调用函数

  • 3
  • 2,875 views

HotNews Pro 2.7版侧边TAB菜单中的本月排行及年度排行,是通过判断日志评论数多少调用显示,如果安装并已启用WP-PostViews统计插件,可以利用WP-PostViews浏览统计功能,调用显示浏览次数最多的日志。

其实这个调用热门文章的方法,在HotNews主题较早的版本中已采用,只不过当时用的是另一款统计插件WP-PostViews Plus。最近有童鞋问如何根据日志点击数调用热门文章,下面就以首页TAB菜单中的年度排行为例,教大家实现这一功能替换。

WP-PostViews插件默认调用浏览次数最多日志的函数为:

  1. <?php if (function_exists('get_most_viewed')): ?>
  2. <?php get_most_viewed(); ?>
  3. <?php endif; ?>

参考:http://wordpress.org/extend/plugins/wp-postviews/faq/

无时间段限制,不是我们想要的,但最新版的WP-PostViews插件确实无显示某时间段内日志的功能,不过早期版本中有相关的函数,下面是从1.11版本中复制出来的相关代码,加到WP-PostViews插件WP-PostViews.php中:

  1. ### Function: Get TimeSpan Most Viewed - Added by Paolo Tagliaferri (http://www.vortexmind.net - webmaster@vortexmind.net)
  2. function get_timespan_most_viewed($mode = '', $limit = 10, $days = 7, $display = true) {
  3. global$wpdb, $post;
  4. $limit_date = current_time('timestamp') - ($days*86400);
  5. $limit_date = date("Y-m-d H:i:s",$limit_date);
  6. $where = '';
  7. $temp = '';
  8. if(!empty($mode) && $mode != 'both') {
  9. $where = "post_type = '$mode'";
  10. } else {
  11. $where = '1=1';
  12. }
  13. $most_viewed = $wpdb->get_results("SELECT $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 < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
  14. if($most_viewed) {
  15. foreach ($most_viewedas$post) {
  16. $post_title = get_the_title();
  17. $post_views = intval($post->views);
  18. $post_views = number_format($post_views);
  19. $temp .= "<li><a href=\"".get_permalink()."\">$post_title</a>".__('', 'wp-postviews')."</li>";
  20. }
  21. } else {
  22. $temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
  23. }
  24. if($display) {
  25. echo$temp;
  26. } else {
  27. return$temp;
  28. }
  29. }

打开HotNewspro\includes目录中tab_h.php(该切换菜单只显示在首页,主题中还有另一个tab.php显示在其它页,替换方法类似),查找

  1. <?php simple_get_most_vieweds(); ?>

替换为:

  1. <?php if (function_exists('get_most_viewed')): ?>
  2. <?php get_timespan_most_viewed('post',10,100, true, true); ?>
  3. <?php endif; ?>

其中:

post:显示单篇日志,若留空则显示单篇日志+页面,若填page则只显示页面。

10:显示的日志数量。

100:只显示100天内的日志,可酌情更改。

true:显示日志,若改为false则不显示文章。

false:不显示搜寻引擎机器人的查询次数,若改为true则全部显示

补充:调用某分类热门文章:

  1. <?php if (function_exists('get_most_viewed_category')): ?>
  2. <?php get_most_viewed_category(3, 'post', 10); ?>
  3. <?php endif; ?>

其中:数字3为分类ID,数字10为日志篇数。

最后,还而要给显示的日志标题加上文字截断,以免由于文字较多时出现回行,影响页面美观,打开主题样式文件style.css,查找:

  1. .tab_latest ul li {
  2. width:212px;
  3. line-height:30px;
  4. border-bottom:1pxdashed#ccc;
  5. margin-bottom:-3px;
  6. }

替换为:

  1. .tab_latest ul li {
  2. width:212px;
  3. line-height:30px;
  4. border-bottom:1pxdashed#ccc;
  5. margin-bottom:-3px;
  6. height:30px;
  7. overflow: hidden;
  8. }

如果要显示全部标题可以不加。

通过上面的函数替换,显示的热门排行可能更为合理一些。

米粒在线
  • 本文由 发表于 2013年6月27日20:53:03
  • 转载请务必保留本文链接:https://www.miliol.org/8381.html
网站建设

WordPress上传文件自动重命名

推荐两段在上传媒体文件时自动重命名的代码。        代码一,按时间重命名 上传文件时会以“年月日时分秒+千位毫秒整数”的格式重命名文件,如“20161023122221765.jpg” ...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

评论:3   其中:访客  3   博主  0
    • 避风港 避风港 1

      不错,内容很强大 :mrgreen:

      • yzf yzf 4

        你的排名疑似掉了6万。