如何创建自定义帖子视图数

时间:2017-07-22 作者:Carlos Stevanato

我正在创建一个论坛插件,需要计算有多少人看到了某个主题(post\\u类型自定义),理想的情况是创建一个新表或直接使用wordpress创建一个表单。

1 个回复
SO网友:vijaysinh vadher

将此添加到函数中。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 View";
    }
    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);
    }
}

结束

相关推荐

Making post views as sortable

我正在wordpress管理帖子页面上使用自定义字段,添加以下代码,然后尝试根据帖子视图对帖子进行排序,但排序不起作用。下面是我在函数中添加的代码。php:add_filter(\'manage_posts_columns\', \'posts_column_views\'); add_action(\'manage_posts_custom_column\', \'posts_custom_column_views\',5,2); function posts_column_views(