WordPress獲得文章瀏覽次數及擷取瀏覽次數最多的文章

來源:互聯網
上載者:User

純程式碼統計文章瀏覽次數

1.在主題的 functions.php檔案的最後一個 ?> 前面添加下面的代碼:

 代碼如下 複製代碼
function record_visitors()  
{  
    if (is_singular())  
    {  
      global $post;  
      $post_ID = $post->ID;  
      if($post_ID)  
      {  
          $post_views = (int)get_post_meta($post_ID, 'views', true);  
          if(!update_post_meta($post_ID, 'views', ($post_views+1)))  
          {  
            add_post_meta($post_ID, 'views', 1, true);  
          }  
      }  
    }  
}  
add_action('wp_head', 'record_visitors');  
/// 函數名稱:post_views  
/// 函數作用:取得文章的閱讀次數  
function post_views($before = '(點擊 ', $after = ' 次)', $echo = 1)  
{  
  global $post;  
  $post_ID = $post->ID;  
  $views = (int)get_post_meta($post_ID, 'views', true);  
  if ($echo) echo $before, number_format($views), $after;  
  else return $views;  
} 

2.在需要顯示該統計次數的地方使用下面的代碼調用:

文章被閱讀:<?php post_views(' ', ' 次'); ?> 

擷取瀏覽次數最多的文章

這個一般來說,就是用來獲得熱門文章了.如果要擷取上面的函數統計出來的瀏覽次數最多的文章,可以在 functions.php檔案的最後一個 ?> 前面添加下面的代碼:

 代碼如下 複製代碼

/// 函數作用:取得閱讀最多的文章  
function get_most_viewed_format($mode = '', $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') {  
  global $wpdb, $post;  
  $output = '';  
  $mode = ($mode == '') ? 'post' : $mode;  
  $type_sql = ($mode != 'both') ? "AND post_type='$mode'" : '';  
  $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" : '');  
  $term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : '';  
  $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)" : '';  
  // database query  
  $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");  
  if ($most_viewed) {  
   foreach ($most_viewed as $viewed) {  
    $post_ID    = $viewed->ID;  
    $post_views = number_format($viewed->views);  
    $post_title = esc_attr($viewed->post_title);  
    $get_permalink = esc_attr(get_permalink($post_ID));  
    $output .= "<li>$beforetitle$post_title$aftertitle";  
    if ($show_date) {  
      $posted = date(get_option('date_format'), strtotime($viewed->post_date));  
      $output .= "$beforedate $posted $afterdate";  
    }  
    $output .= "$beforecount $post_views $aftercount</li>";  
   }  
  } else {  
   $output = "<li>N/A</li>n";  
  }  
  echo $output;  
} 

然後使用下面的函數調用:

 代碼如下 複製代碼

<?php get_most_viewed_format(); ?> 

小結

這裡部落需要補充一下的是,用上面的方法獲得的文章瀏覽次數統計,與WP-Postviews 外掛程式是有所差別的,這裡的代碼擷取的,其實就連搜尋引擎爬蟲也會進行統計.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.