duanjiaren8188 2019-06-18 10:29
浏览 43

如何查看是否已查看帖子或属性(在每个会话中)

I'm using RealHomes Wordpress theme and I want to remember the clicks on each property, but only in one session.

For example, I go to a website to look at some properties, and I click on a few of them, and later when I refresh the page it would say that those properties have been seen, maybe just a text saying "Seen".

And later, or tomorrow, when I go to that same page, all those properties shouldn't have a message "Seen", I want to reset the clicks/views.

I have these two functions in my functions.php file, the getPostViews can be called where you want to display the counter, and setPostViews is called on the beginning of the single.php property file.

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

    add_action('end_session_action','end_session'); 
}

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

I desided to display the clicks on the homepage, next to each property.

Image: property has been seen

Image: property hasn't been seen

My question is, how can I reset this meta values on each new session?

I also found this code that displays the last time a user visited a website, it resets the clock everytime.

function viewed_cookie($postID) {

    // Time of user's visit
    date_default_timezone_set("Europe/Ljubljana");
    $visit_time = date('d.m.Y. G:i');

    // Check if cookie is already set
    if(isset($_COOKIE['visited_cookie'])) {
        // Do this if cookie is set 
        function postViewed() {

            // Use information stored in the cookie 
            $lastvisit = $_COOKIE['visited_cookie'];
            $string .= $lastvisit; 

            // Delete the old cookie so that we can set it again with updated info
            unset($_COOKIE['visited_cookie']); 

            return $string;
        }   
    } else { 
        // Do this if the cookie doesn't exist
        function postViewed() { 
            $string .= 'New here? Check out these resources...' ;
            return $string;
        }   
    }

    add_shortcode('viewed', 'postViewed');

    // Set or Reset the cookie
    setcookie('visited_cookie',  $visit_time);

}
add_action('init', 'viewed_cookie'); 

How can I merge the set/get functions and use a cookie like in the last example?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行