wordpress中怎么实现网站统计

2024-11-20 08:58:32

1、首先在所使用主题文件目录中找到functions.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 "; } return $count;}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); }}

2、功能代码添加好后,我们开始进行统计,在要计算的网页中添加如下代码:<?php setPostViews(get_the_ID()); ?>

3、统计代码添加好后,接着便是进行调用统计次数,我们可以在任何地方进行添加(一般在footer.php、index.php文件等)。<?php echo getPostViews(get_the_ID()); ?> 次浏览

猜你喜欢