我用的是2017主题。该主题允许根据网站页面在主页上有不同的部分。我想知道我如何才能添加并使其只让已注销的用户在其中一个部分中看到某个页面?基本上,我有一个新的用户注册页面,我想作为我的一个部分,但我不想让登录用户看到该部分,只想让注销用户看到。
如何隐藏文章元素,以便只有注销的用户才能看到它
您可以通过检查用户是否按照第一个答案中的说明登录来完成此操作。你只需要换一种方式。
例如,如果菜单中有一个链接,如果用户登录,该链接应该隐藏,只需添加一个自定义css;
.logged_in .menu-class-here{display:none;}
即隐藏菜单项。现在,如果要在用户登录时限制对页面的访问,只需执行重定向即可。这里有一个例子;if ( is_page(\'slug\') && is_user_logged_in() ) { // where slug is the name or slug of the custom page that you want to restrict from logged in users
wp_redirect( \'http://www.example.com/desired-page/\', 301 );
exit;
}
如果您想允许访问该页面,但对登录用户隐藏某个部分,可以执行以下操作:;if ( is_page(\'slug\') && ! is_user_logged_in() ) {
//add the code here that you want to show to non-logged-in users
}
UPDATE将此添加到函数。php。您需要创建一个函数来重定向和添加它function notallowed() {
global $post;
if ( is_page(\'hire-the-freelancer\') && is_user_logged_in() ) { // where slug is the name or slug of the custom page that you want to restrict from logged in users
wp_redirect( \'http://www.example.com/desired-page/\', 301 );
exit;
}
}
add_action( \'template_redirect\', \'notallowed\' );
您可以使用内置is_user_logged_in wordpress函数:)
if (!is_user_logged_in()) {
// content for non-authenticated users
}