最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - How do I hide an article element so only logged out users see it

programmeradmin1浏览0评论

I am using twenty seventeen theme. The theme allows to have different sections on homepage based on site pages. I was wondering how I can add make it so only logged out users see a certain page in one of the sections? Basically I have a new user registration page i want to have as one of my sections but I don't want logged in users to see that section, only logged out users.

My page is jonathanmusiclessons

I am using twenty seventeen theme. The theme allows to have different sections on homepage based on site pages. I was wondering how I can add make it so only logged out users see a certain page in one of the sections? Basically I have a new user registration page i want to have as one of my sections but I don't want logged in users to see that section, only logged out users.

My page is jonathanmusiclessons

Share Improve this question asked Sep 6, 2017 at 16:21 JonathanJonathan 11 silver badge1 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 1

You can do this by checking if the user is logged in as stated in the first answer. You simply need to do it the other way.

For example, if you have a link in the menu that should be hidden in case the user is logged in, just add a custom css;

.logged_in .menu-class-here{display:none;}

That is to hide a menu item. Now, if you want to restrict the access to a page if the user is logged in, you can simply do a redirect. Here's an example;

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/desired-page/', 301 ); 
   exit;
}

If you want to allow access to the page but hide a certain section from logged in users, you can do something like;

if ( is_page('slug') && ! is_user_logged_in() ) {
  //add the code here that you want to show to non-logged-in users
}

UPDATE Add this to functions.php. You need to create a function to redirect and add it

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/desired-page/', 301 ); 
   exit;
}
}
add_action( 'template_redirect', 'notallowed' );

You can solve your problem by using the built-in is_user_logged_in wordpress function :)

if (!is_user_logged_in()) {
  // content for non-authenticated users
}
发布评论

评论列表(0)

  1. 暂无评论