Wordpress 如何在x个访客数后显示模式

Wordpress 如何在x个访客数后显示模式,wordpress,bootstrap-modal,webpage,Wordpress,Bootstrap Modal,Webpage,我正在尝试每50个用户弹出一个模式,用于站点调查。我可以很容易地在我需要的页面上创建一个模式,但无法找到一种方法,让它只在x次访问后显示。有人能给我指出正确的方向吗?在主题的functions.php文件中编写以下代码: function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==

我正在尝试每50个用户弹出一个模式,用于站点调查。我可以很容易地在我需要的页面上创建一个模式,但无法找到一种方法,让它只在x次访问后显示。有人能给我指出正确的方向吗?

在主题的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);
    }
}

// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
这部分代码将设置post视图。只需将下面的代码片段放在您的文件中

<?php
    setPostViews(get_the_ID());
?>

下面的代码显示了帖子中的视图数。因此,编写如下代码:

<?php
    if(getPostViews(get_the_ID()) >= 'x') {
        // Write your popup code here
    }
?>

使用PHP和MySQL,统计访客数量。