代码实现WordPress文章浏览次数统计
如何实现WordPress文章浏览次数统计?这个功能非常实用,如何用代码来实现这一功能,当然你也可以使用插件来实现同样的功能。
如何实现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
版权所有 © 转载时必须以链接形式注明作者和原始出处!