帖子浏览量/点击计数器问题?

时间:2015-06-29 作者:Nilohn

我已经决定使用WordPress作为我5个月前的网站的CMS。我已经在我的新网站(WordPress)上添加了所有以前的帖子。但现在我需要将视图计数更改为以前的视图计数。我找不到hit_countviews_count 来自PhpMyAdmin数据库。如何在WordPress上手动更改视图计数?请帮忙。

作用php

<?php
// function to display number of posts.
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.\' Views\';
}

// function to count views.
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 it to a column in WP-Admin - (Optional)
add_filter(\'manage_posts_columns\', \'posts_column_views\');
add_action(\'manage_posts_custom_column\', \'posts_custom_column_views\',5,2);
function posts_column_views($defaults){
    $defaults[\'post_views\'] = __(\'Views\');
    return $defaults;
}
function posts_custom_column_views($column_name, $id){
 if($column_name === \'post_views\'){
        echo getPostViews(get_the_ID());
    }
}
?>
<小时>单程。php

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

1 个回复
最合适的回答,由SO网友:Karun 整理而成

使用以下功能手动设置视图

function setPostViewsManually($postID, $viewCount) {
    $count_key = \'post_views_count\';
    $count = get_post_meta($postID, $count_key, true);
    update_post_meta($postID, $count_key, $viewCount);
}
您可以通过以下方式运行此代码

<?php setPostViewsManually(get_the_ID(), 1234); ?>
以上代码将当前帖子的视图设置为“1234”。小心地运行此函数,否则您的新视图将始终为“1234”,因为此函数每次都将保持运行。

只需使用一次并删除代码即可。

结束

相关推荐

Count posts for each year

我用下面的代码列出我博客中的所有年份,我想我们可以称之为归档。<?php $years = $wpdb->get_col(\"SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC\"); foreach($years as $year) : ?> <?php echo $year ?> <?php endforeach; ?>