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

首頁 > CMS > Wordpress > 正文

wordpress文章瀏覽量的實現方法

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

我們在網上找到很多使用插件來實現,但個人學喜歡插件怕不安全或代碼太多了,后來找到一個朋友寫的一段代碼,wordpress不用插件來實現功能方法.

第一種,找到functions模板,加入以下代碼:

  1. function getPostViews($postID){ 
  2. $count_key = ‘post_views_count’; 
  3. $count = get_post_meta($postID$count_key, true); 
  4. if($count==”){ 
  5. delete_post_meta($postID$count_key); 
  6. add_post_meta($postID$count_key, ’0′); 
  7. return “0 View”; 
  8. return $count.’ Views’; 
  9. function setPostViews($postID) { 
  10. $count_key = ‘post_views_count’; 
  11. $count = get_post_meta($postID$count_key, true); 
  12. if($count==”){ 
  13. $count = 0; 
  14. delete_post_meta($postID$count_key); 
  15. add_post_meta($postID$count_key, ’0′); 
  16. }else
  17. $count++; 
  18. update_post_meta($postID$count_key$count); 

然后將下面代碼加到主題single模版主循環的中,代碼如下:

<?php setPostViews(get_the_ID()); ?>

也就是類似這句的下面,代碼如下:

<?php if (have_posts()):while (have_posts()):the_post(); ?>

最后,將調用顯示閱讀次數代碼加到single模版適當的位置,代碼如下:

<?php echo getPostViews(get_the_ID()); ?>

如果想在其它位置顯示閱讀次數,可以將下面代碼也加到functions模版中:

remove_action(’wp_head’,’adjacent_posts_rel_link_wp_head’,10,0);

這樣就實現了wordpress不用插件來顯示文章瀏覽量的功能了.

第二種,比較簡單—找到functions模板,加入以下代碼:

  1. //postviews 
  2. function get_post_views ($post_id) { 
  3.  
  4. $count_key = ‘views’; 
  5. $count = get_post_meta($post_id$count_key, true); 
  6.  
  7. if ($count == ”) { 
  8. delete_post_meta($post_id$count_key); 
  9. add_post_meta($post_id$count_key, ’0′); 
  10. $count = ’0′; 
  11.  
  12. echo number_format_i18n($count); 
  13.  
  14.  
  15. function set_post_views () { 
  16.  
  17. global $post
  18.  
  19. $post_id = $post -> ID; 
  20. $count_key = ‘views’; 
  21. $count = get_post_meta($post_id$count_key, true); 
  22.  
  23. if (is_single() || is_page()) { 
  24.  
  25. if ($count == ”) { 
  26. delete_post_meta($post_id$count_key); 
  27. add_post_meta($post_id$count_key, ’0′); 
  28. else { 
  29. update_post_meta($post_id$count_key$count + 1); 
  30.  
  31.  
  32. add_action(‘get_header’, ‘set_post_views’); 

加入到主題functions模版文件中,直接調用<?php get_post_views($post -> ID); ?> views 到文章頁面即可.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘肃省| 察隅县| 万山特区| 黄冈市| 湟中县| 方正县| 新安县| 都安| 南华县| 延安市| 环江| 扬中市| 湘潭县| 红河县| 巴林左旗| 凤冈县| 韶关市| 丹巴县| 鄂尔多斯市| 炎陵县| 额敏县| 佛学| 大渡口区| 满洲里市| 乾安县| 石门县| 南靖县| 镇原县| 兴文县| 盘山县| 岑溪市| 宝鸡市| 丹东市| 合川市| 永胜县| 金堂县| 潞城市| 广昌县| 轮台县| 师宗县| 皋兰县|