国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

Wordpress實現php代碼版文章瀏覽數

2024-09-07 00:50:22
字體:
來源:轉載
供稿:網友

本文章下面來給各位同學介紹一下Wordpress實現php代碼版文章瀏覽數,這里就可以瀏覽到每篇文章的具體瀏覽資料了,如果需要更詳細的我們可使用WP-Postviews 這款插件來統計文章瀏覽數了.

我個人只要“統計和輸出瀏覽數”功能,所以挪用了主體函數部分,代碼如下:

  1. function custom_the_views($post_id$echo=true, $unit=' views') {  
  2.     $count_key = 'views';  
  3.     $views = get_post_custom($post_id);  
  4.     $views = intval($views['views'][0]);  
  5.     if ($views == '') {  
  6.         return '';  
  7.     } else {  
  8.         if ($echo) {  
  9.             echo number_format_i18n($views) . $unit;  
  10.         } else {  
  11.             return number_format_i18n($views) . $unit;  
  12.         }  
  13.     }  
  14. }  
  15. function set_post_views() {  
  16.     global $post;  
  17.     $post_id = intval($post->ID);  
  18.     $count_key = 'views';  
  19.     $views = get_post_custom($post_id);  
  20.     $views = intval($views['views'][0]);  
  21.     if (is_single() || is_page()) {  
  22.         if(!update_post_meta($post_id'views', ($views + 1))) {  
  23.             add_post_meta($post_id'views', 1, true);  
  24.         }  
  25.     }  
  26. }  
  27. add_action('get_header''set_post_views'); 

參數說明:

1. $post_id: 文章ID,一般填 $post->ID

2. $echo: true/false,顯示/獲取瀏覽數(默認是顯示-true)

3. $views: 瀏覽數單位(默認是:空格+views)

一般調用方法,代碼如下:

<?php if ( function_exists('custom_the_views') ) custom_the_views($post->ID);?> 

提示:如果原來用了 WP-Postviews 這個插件,只要把如下代碼:

<?php the_views(); ?> 改為 <?php custom_the_views($post->ID); ?>即可。

或者是如下代碼:

<?php if ( function_exists('the_views') ) the_views(); ?>

改為:<?php if ( function_exists('custom_the_views') ) custom_the_views($post->ID);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙陵县| 金华市| 房产| 漳平市| 家居| 罗定市| 桂平市| 塔河县| 宣汉县| 平南县| 克拉玛依市| 津南区| 六枝特区| 淄博市| 广宁县| 日喀则市| 长海县| 巨鹿县| 囊谦县| 新和县| 丹巴县| 盘山县| 团风县| 平湖市| 越西县| 凌源市| 杭锦旗| 灵丘县| 赤壁市| 乐安县| 美姑县| 云南省| 唐河县| 天气| 陇西县| 凤冈县| 房产| 漠河县| 麻城市| 通江县| 疏附县|