王尘宇王尘宇

研究百度干SEO做推广变成一个被互联网搞的人

代码完成wordpress阅读统计功用,省去运用WP-PostViews插件!

之前小编照样小白的时刻,玩wordpress主题的时刻常常喜好装置一些插件来增添wordpress的功用性,比如说wordpress浏览统计功用,比如说WP-PostViews插件。。。以后发明,插件装置的越多,wordpress速率越来越慢,以后经过分娘的查询后发明,装置过量的插件对wordpress主题速率影响非常大,由于会发生许多的查询,造成了数据变慢,而近来小编的网站正在逐渐的代码化了,把能够代码完成尽量的去代码完成,wordpress瘦身,从wordpress插件的削减运用入手下手!

那末本日小编要讲的就是经由过程代码完成wordpress浏览统计功用,就不谈运用别的主题,就小编正在运用的知更鸟主题来讲,就有一个必需要装置的插件WP-PostViews,而且还不能升级,小编每次看到wordpress的升级按钮就很恼火。本日闲来无事,捣鼓了一番,整理出来了解决方案,下面说重点了!

基础我们运用WP-PostViews插件都是想有统计和浏览数的功用,那末下面的一段代码就是来完成这两个功用的!

首先在寻找到functions.php.php文件夹,在末了面  ?> 的前面到场下面的代码

  1. /* 接见计数 */

  2. function record_visitors()

  3. {

  4.     if (is_singular())

  5.     {

  6.       global $post;

  7.       $post_ID = $post->ID;

  8.       if($post_ID)

  9.       {

  10.           $post_views = (int)get_post_meta($post_ID, 'views', true);

  11.           if(!update_post_meta($post_ID, 'views', ($post_views+1)))

  12.           {

  13.             add_post_meta($post_ID, 'views', 1, true);

  14.           }

  15.       }

  16.     }

  17. }

  18. add_action('wp_head', 'record_visitors');


  19. /// 函数称号:post_views

  20. /// 函数作用:获得文章的浏览次数

  21. function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)

  22. {

  23.   global $post;

  24.   $post_ID = $post->ID;

  25.   $views = (int)get_post_meta($post_ID, 'views', true);

  26.   if ($echo) echo $before, number_format($views), $after;

  27.   else return $views;

  28. }

代码放好后,保留,然后到主题前端位置增加挪用

  1. 浏览:<?php post_views(' ', ' 次'); ?>

猎取浏览次数最多的文章
假如要猎取上面的函数统计出来的浏览次数最多的文章,能够在 functions.php文件的末了一个 ?> 前面增加下面的代码:

  1. /// get_most_viewed_format

  2. /// 函数作用:获得浏览最多的文章

  3. function get_most_viewed_format($mode = '', $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') {

  4.   global $wpdb, $post;

  5.   $output = '';

  6.   $mode = ($mode == '') ? 'post' : $mode;

  7.   $type_sql = ($mode != 'both') ? "AND post_type='$mode'" : '';

  8.   $term_sql = (is_array($term_id)) ? "AND $wpdb->term_taxonomy.term_id IN (" . join(',', $term_id) . ')' : ($term_id != 0 ? "AND $wpdb->term_taxonomy.term_id = $term_id" : '');

  9.   $term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : '';

  10.   $inr_join = $term_id ? "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)" : '';


  11.   // database query

  12.   $most_viewed = $wpdb->get_results("SELECT ID, post_date, post_title, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) $inr_join WHERE post_status = 'publish' AND post_password = '' $term_sql $type_sql AND meta_key = 'views' GROUP BY ID ORDER BY views DESC LIMIT $limit");

  13.   if ($most_viewed) {

  14.    foreach ($most_viewed as $viewed) {

  15.     $post_ID    = $viewed->ID;

  16.     $post_views = number_format($viewed->views);

  17.     $post_title = esc_attr($viewed->post_title);

  18.     $get_permalink = esc_attr(get_permalink($post_ID));

  19.     $output .= "<li>$beforetitle$post_title$aftertitle";

  20.     if ($show_date) {

  21.       $posted = date(get_option('date_format'), strtotime($viewed->post_date));

  22.       $output .= "$beforedate $posted $afterdate";

  23.     }

  24.     $output .= "$beforecount $post_views $aftercount</li>";

  25.    }

  26.   } else {

  27.    $output = "<li>N/A</li>n";

  28.   }

  29.   echo $output;

  30. }

然后运用下面的函数挪用:

  1. <?php get_most_viewed_format(); ?>

怎样,很简单吧。。。有什么疑问本帖复兴解答!

相关文章

评论列表

发表评论:
验证码

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。