设置刷新浏览器不影响WordPress点击阅读量实现方法

我们在给WordPress添加功能的时候尽量的少用插件降低WP的负载。我们有些设计主题的不够严谨,点击阅读功能是刷新浏览器就自动增加,这个不是正常的用户阅读数。我们如何调整更为严谨的WordPress点击阅读量,这里我们可以修改。

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);
    }
}

添加到Functions.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);//设置时间间隔
    }
?>

将代码放到有阅读数的页面上面。

5/5 - (1 vote)
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享