WordPress非外掛程式添加文章瀏覽次數統計功能

本文目錄1非外掛統計文章瀏覽次數2獲取瀏覽次數最多的文章

WordPress文章瀏覽次數統計功能是必不可少的,不少主題已經集成該功能,如果你的主題沒有集成,你可以使用 WP-Postviews 外掛,或者試試本文的代碼。

WordPress非外掛實現文章瀏覽次數統計的方法,是DH參考willin kan大師的my_visitor外掛來寫的,刷新一次文章頁面就統計一次,比較簡單實用。

非外掛統計文章瀏覽次數

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 訪問計數 */
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;
}

/* 訪問計數 */
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.在需要顯示該統計次數的地方使用下面的代碼調用:

1
閱讀:<?php post_views(' ', ' 次'); ?>

閱讀:<?php post_views(‘ ‘, ‘ 次’); ?>

獲取瀏覽次數最多的文章

如果要獲取上面的函數統計出來的瀏覽次數最多的文章,可以在 functions.php文件的最後一個 ?> 前面添加下面的代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// get_most_viewed_format
/// 函數作用:取得閱讀最多的文章
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;
}

/// get_most_viewed_format
/// 函數作用:取得閱讀最多的文章
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;
}

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

1
<?php get_most_viewed_format(); ?>

<?php get_most_viewed_format(); ?>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *