• 微信号:Zh-y9213
您当前的位置:爱资源分享网 > > 网站源码 > 教程合集

WordPress文章刷新不影响浏览量的统计代码

栏目:教程合集时间:2021-11-23 14:31阅读数:58人阅读

wordpress程序默认是没有文章浏览器统计的,无论是用纯代码还是使用wordpress插件wp-postview,刷新当前页面还是会算做一个浏览数量,那今天给大家介绍下刷新不重复记录的文章浏览量统计代码。

一、在wordpress主题下functions.php里增加以下代码


//add by charleswu
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);
        }
}
 

二、解决刷新统计数增加,一定要放在文章页面的最前面,貌似php设置cookie之前不能有输出,我的是single.php页面


<?php
        $post_id=get_the_ID();
        if(isset($_COOKIE['views'.$post_id.COOKIEHASH]) && $_COOKIE['views'.$post_id.COOKIEHASH] == '1')
        {
                       
        }
        else{
                setPostViews($post_id);
                setcookie('views'.$post_id.COOKIEHASH,'1',time() + 3600,COOKIEPATH,COOKIE_DOMAIN);//设置时间间隔
        }
?>
 

其实第一段代码和网上能找到的普通wordpress文章浏览量添加代码是一样的,重点是第二段的需要在文章页面single里面添加的这段代码起着决定性的作用。大家可以使用看看,有更多的方式可以和我沟通。


资源均来自第三方,谨慎下载,前往第三方网站下载

本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:737597453@qq.com