1
votes

How to hide certain categories from 'not logged in users' in Wordpress blog? And redirect these 'not logged in users' to the login screen, and after login back to the post page?

All posts from that private category should be hidden (over the entire blog) for not logged in users. No messages, just completely not visible.

I tried to add this code to the funcions.php:

<?php
if ( is_user_logged_in() ) {
query_posts();
}
else{
query_posts( cat= -1 );
}
?>

But that shows a blank page on admin and front end.

Is there anybody who can help me how to do this? There should be a plugin for this, but there is none. Any help will be welcome. Thanks!

2
Hi val, welcome to SO. Could you edit you're question and share what you've already tried? It will help give you an answer. - Lea Cohen
I tried to add this code to the funcions.php: <?php if ( is_user_logged_in() ) { query_posts(); } else{ query_posts( cat='-1 ); } ?> For redirecting to login I use a plugin now. I first wanted to try to hide a certain category for non logged in users. - val

2 Answers

0
votes

Wordpress has a very simple conditional tag for to not display content if a user is not logged in:

<?php
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}
?>

This will work if you are editing your own template.

Otherwise this plugin is great.

To redirect users after log in use this plugin, also great!

0
votes

Ah....it looks like the code is to put in the Loop, not in function.php. I will ask a new question about how to create a function that hides a certain category if user is not logged in. Thank you for your time and suggestions!