一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

WordPress设置只有注册用户才能浏览特定的内容

时间:2013-02-16 编辑:简简单单 来源:一聚教程网

我们增加一个自定义字段:user_only,如果这个值不为零,这这篇日志或者页面是只能给注册用户浏览,然后通过 the_content 来控制内容显示,这样就能简单的并且灵活设置具体到哪篇文章或者页面是只能注册用户浏览。详细代码如下:

 

 代码如下 复制代码
add_filter('the_content', 'post_user_only');
function post_user_only($text){
    global $post;
   
    $user_only = get_post_meta($post->ID, 'user_only', true);
    if($user_only){
        global $user_ID;
        if(!$user_ID){
            $redirect = get_permalink($post->ID);
            $text = '该内容仅限于会员浏览,请登录!';
        }
    }
    return $text;
}
?>

把上面带复制成一个文件上传到插件目录,激活即可

热门栏目