2017-02-22 11 views
0

サイト調査のために50人のユーザーごとにモーダルポップアップを表示しようとしています。私は必要なページで簡単にモーダルを作成することができますが、x回の訪問の後にしか表示されないようにする方法を見つけられません。誰かが私を正しい方向に向けることができますか?x人の訪問者の後にモーダルを表示する方法

+0

PHPとMySQLを使用して訪問者を数えます。 – cgee

答えて

0

は、あなたのテーマのの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); 

コードのこの部分は、ポストビューを設定します。このスニペットをファイルの下に置きます。

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

以下のコードは、あなたの投稿内の閲覧回数を表示しています。したがって、次のようなコードを書く:

<?php 
    if(getPostViews(get_the_ID()) >= 'x') { 
     // Write your popup code here 
    } 
?> 
関連する問題