wordpress将某个文章设置为游客需要登录才可以访问(整篇访问限制) wordpress


把下面的代码添加到functions.php的最下面。然后在编辑文章的最右上角点击显示选项,勾选自定义栏目,在底部添加自定义栏目中点选输入新栏目输名称post_user_only,输入值1,即可.

*
*设置不允许游客访问某些内容。
*然后在文章右上角点"显示选项",勾选"自定义栏目",在底部"添加自定义栏目"中点选"输入新栏目"输名称"post_user_only",输入值"1",即可。
*/
add_filter('the_content','post_user_only');
function post_user_only($text) {
global $post;
$post_user_only = get_post_meta($post->ID,'post_user_only',true);
if($post_user_only) {
global $user_ID;
if(!$user_ID) {
$redirect = urlencode(get_permalink($post->ID));
$text = '<div class="alert alert-danger">该资源<a href="/wp-login.php?redirect_to='.$redirect.'" target="_blank" rel="nofollow"><span style="color: #3366ff;">登录</span></a>后才可访问,如果您没有账号,可以先进行<a href="/wp-login.php?action=register"><span style="color: #3366ff;">注册</span></a>。</div>';
}
}
return $text;
}


签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回