Wordpress后视图计数不同

时间:2014-05-07 06:03:08

标签: php wordpress

我遇到了查看计数的问题。 主页显示帖子被观看了16次但是当我进入(单)帖子。它只显示我10。计数更新似乎略有延迟。

我正在使用CloudFlare服务。但这不应成为问题的原因。 怀疑是缓存问题。但我不知道如何解决它。 有人遇到像我这样的问题,并有一些解决方案吗?

这是我在function.php

中找到的源代码
    if ( !function_exists( 'getPostViews' ) ) {
    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. __('','color-theme-framework');
    }
}

if ( !function_exists( 'setPostViews' ) ) {
    function setPostViews($postID) {
    if (!current_user_can('administrator') ) :
        $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);
        }
    endif;
    }
}

if ( !function_exists( 'posts_column_views' ) ) {
    function posts_column_views($defaults){
        $defaults['post_views'] = __( 'Views' , 'color-theme-framework' );
        return $defaults;
    }
}

if ( !function_exists( 'posts_custom_column_views' ) ) {
    function posts_custom_column_views($column_name, $id){
        if( $column_name === 'post_views' ) {
            echo getPostViews( get_the_ID() );
        }
    }
}

add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);

1 个答案:

答案 0 :(得分:0)

这是我用来在我的网站上显示帖子视图的代码。请注意,管理员视图不计算在内,如果有人在12小时内转到同一个帖子,则不会计算第二个视图。仅当该人在12小时后再次访问该页面时,才会注册第二个视图计数。因此,这是计算帖子视图的更准确方法

if ( ! function_exists( 'pietergoosen_get_post_views' ) ) :

function pietergoosen_get_post_views($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;
}

endif;

// function to count views.
if ( ! function_exists( 'pietergoosen_update_post_views' ) ) :

function pietergoosen_update_post_views($postID) {
    if( !current_user_can('administrator') ) {
        $user_ip = $_SERVER['REMOTE_ADDR']; //retrieve the current IP address of the visitor
        $key = $user_ip . 'x' . $postID; //combine post ID & IP to form unique key
        $value = array($user_ip, $postID); // store post ID & IP as separate values (see note)
        $visited = get_transient($key); //get transient and store in variable

        //check to see if the Post ID/IP ($key) address is currently stored as a transient
        if ( false === ( $visited ) ) {

            //store the unique key, Post ID & IP address for 12 hours if it does not exist
           set_transient( $key, $value, 60*60*12 );

            // now run post views function
            $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);
            }
        }
    }   
}

endif;

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

您现在只需在需要显示计数的地方添加以下代码

即可
<div class="readercount">
                    <?php $views = pietergoosen_get_post_views(get_the_ID());
                        if(pietergoosen_get_post_views(get_the_ID()) == 1) {  
                            printf( __( '%d Reader have read this post.', 'pietergoosen' ) , $views );
                        } else {  
                            printf( __( '%d Readers have read this post.', 'pietergoosen' ) , $views );  
                        }  
                    ?>
            </div>

并在single.php中添加以下代码以注册计数

pietergoosen_update_post_views(get_the_ID());