WordPress:只将内容展示给会员或游客(短代码/简码)

如果你做的站某些内容资源不想被人随便看到,可以使用权限加以管理,在WP里可以使用简码(短代码)来实现,方法很简单。

一、会员

1
2
3
4
5
6
7
8
add_shortcode('USER','show_user_content');
function show_user_content($atts,$content = null){
    global $post;
    if (!is_user_logged_in()){
        return "此内容只展示给已经登录的会员,  " . wp_login_url( get_permalink($post->ID) ) . ' 登录后查看全部内容' ;
    }
    return $content;
}

上面这段代码创建了[USER]这个简码将内容展示给已经登录的会员,未登录的会员则提示需要点击“登录”(链接到登陆页面)。

二、游客

1
2
3
4
5
6
7
add_shortcode('GUEST','show_guest_content');
function show_guest_content($atts,$content){
    if (!is_user_logged_in()){
        return $content;
    }
        return '';
}

这段是创建了[GUEST]这个简码,给游客创建的。

三、使用方法

1
2
3
4
5
6
[GUEST]
这段内容是给游客看的
[/GUEST]
[USER]
会员内容(只公开给已经登录的会员)
[/USER]

结束,如果你做了会员站,那么上面的代码可以应用你的网站上了。

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this post. TrackBack URL