WordPress隐藏文章内容可见

1 判断用户是否为管理员(administrator)

if ( current_user_can (  'manage_options'  )  )  {
    echo  'The current user is a administrator' ;
}

2 判断用户是否为编辑(Editor)

if ( current_user_can (  'publish_pages'  )  &&  ! ​​current_user_can (  'manage_options'  )  )  {
    echo  'The current user is an editor' ;
}

3 判断用户是否为作者(Author)

if ( current_user_can (  'publish_posts'  )  &&  ! ​​current_user_can (  'publish_pages'  )  )  {

    echo  'The current user is an author' ;

}

4 判断用户是否为贡献者(Contributor)

if ( current_user_can (  'edit_posts'  )  &&  ! ​​current_user_can (  'publish_posts'  )  )  { 
    echo  'The current user is a contributor' ; 
}

5 判断用户是否为订阅者(Subscriber)

if ( current_user_can (  'read'  )  &&  ! ​​current_user_can (  'edit_posts'  )  )  { 
    echo  'The current user is a subscriber' ; 
}

访问设置

在 /usr/local/nginx/html/wordpress/wp-content/themes/主题/functions.php 最后添加

//部分内容登录可见
function login_to_read($atts, $content=null) {
    extract(shortcode_atts(array("notice" => '
    <p style="color:red;font-size:23px;text-align: center;">【温馨提示:此处隐藏内容需要<a href="http://www.lxspider.com/wp-login.php" rel="external nofollow" >登录</a>后才能查看!】</p> 
    '), $atts));
    if ( is_user_logged_in() && !is_null( $content ) && !is_feed()  )
                return $content;
        return $notice;
}
add_shortcode('login', 'login_to_read');

// 限定角色访问权限(edit_posts贡献者可见)
function vip_shortcode( $atts, $content = null ) {
    extract(shortcode_atts(array("notice" => '
    <p style="color:red;font-size:23px;text-align: center;">【温馨提示:此处隐藏内容需要<a href="http://www.lxspider.com/?p=72" rel="external nofollow" >付费订阅</a>后才能查看!】</p> 
    '), $atts));
    if ( is_user_logged_in() && (current_user_can('<strong><del>vip</del></strong>')||current_user_can('edit_posts')) && !is_null( $content ) && !is_feed())
             return $content;
       return $notice;
}
add_shortcode('vip', 'vip_shortcode');

使用方法

在编辑器插入标签 :
file

效果示例

【温馨提示:此处隐藏内容需要登录后才能查看!】

【温馨提示:此处隐藏内容需要付费订阅后才能查看!】

点赞

发表回复