国外网赚学习

项目EMU教程

关注的频道
QQ:1565238120

代码实现WordPress文章浏览次数统计

如何实现WordPress文章浏览次数统计?这个功能非常实用,如何用代码来实现这一功能,当然你也可以使用插件来实现同样的功能。

首页 > 建站 > 程序/文档 > 代码实现WordPress文章浏览次数统计 第 359 次浏览

如何实现WordPress文章浏览次数统计?

这个功能非常实用,如何用代码来实现这一功能,当然你也可以使用这个插件如:WP-PostViews 来实现同样的功能。

 

在你使用的博客主题里面,找到function.php,加入以下两段代码(加到最后就好)。

  • //浏览次数统计
  • function getPostViews($postID){ $count_key = ‘post_views_count’; $count = get_post_meta($postID, $count_key, true); if($count==”){ delete_post_meta($postID, $count_key);
  • add_post_meta($postID, $count_key, ‘0’); return “0 View”; } return $count.’ Views’; } function setPostViews($postID) { $count_key = ‘post_views_count’; $count = get_post_meta($postID, $count_key, true);
  • if($count==”){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, ‘0’);
  • }else{ $count++; update_post_meta($postID, $count_key, $count); } }
    //浏览次数统计

在这段代码后面追加以下代码

  • <?php setPostViews(get_the_ID()); ?> <?php echo getPostViews(get_the_ID()); ?>
更新时间: 2022-09-07
本文地址:https://www.lzb6.com/website/program/108.html
置顶