WordPress 文章阅读量、访问量、浏览量统计记录

阅读量统计

在functions.php文件里添加

//文章阅读统计
function post_views_record()
{
    if (is_singular()) {
        global $post;
        $post_ID = $post->ID;
        if ($post_ID) {
            $post_views = (int)get_post_meta($post_ID, 'views', true);
            if (!update_post_meta($post_ID, 'views', ($post_views + 1))) {
                add_post_meta($post_ID, 'views', 1, true);
            }
        }
    }
}

add_action('wp_head', 'post_views_record');
//获取阅读统计
function get_post_views()
{
    global $post;
    $post_ID = $post->ID;
    $views = (int)get_post_meta($post_ID, 'views', true);

    return $views;
}

使用方法

这样直接调用就行了

get_post_views()

在后台展示阅读量

显示在这个位置

同样在functions.php文件里添加

//后台日志阅读统计
add_filter('manage_posts_columns', 'postviews_admin_add_column');
function postviews_admin_add_column($columns)
{
    $columns['views'] = __('阅读');

    return $columns;
}

add_action('manage_posts_custom_column', 'postviews_admin_show', 10, 2);
function postviews_admin_show($column_name, $id)
{
    if ($column_name != 'views') {
        return;
    }
    $post_views = get_post_meta($id, "views", true);
    echo $post_views;
}

WordPress 根据文章阅读量排序 参考这篇文章:https://www.blyoo.com/4270.html