国产探花免费观看_亚洲丰满少妇自慰呻吟_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);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建平县| 东阳市| 文山县| 佛教| 扶绥县| 卓资县| 钦州市| 白山市| 珲春市| 苍溪县| 绥棱县| 嵊泗县| 永吉县| 会宁县| 阳泉市| 北辰区| 麻栗坡县| 奎屯市| 拉孜县| 太谷县| 资阳市| 桦川县| 六盘水市| 蚌埠市| 商丘市| 吕梁市| 渝中区| 阿拉善右旗| 宜州市| 斗六市| 河北省| 博客| 疏附县| 宝清县| 涞水县| 共和县| 陆良县| 出国| 辽源市| 临武县| 大厂|