WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making use of this custom code to my loop.php file to restrict non-registered members access to posts from a certain category (cat 1)

<?php if (is_user_logged_in() && $cat == '1') { ?> 
<?php the_content(); ?> 
<?php } else { ?> 
<?php the_excerpt(); ?>
<?php } ?> 

However this piece of code doesn't help much as it restricts all posts from other categories as well.

Any suggestions?

share|improve this question

try this:

    //first check if its the category in question
    if ($cat == '1') { 
        //then if the user is logged in show the content
        if (is_user_logged_in()){
        }else{ //if not show the excerpt
    }else{ // and if its not the category show the content


Judging by your comments you are some defining $cat before the check so add:

$cat = get_query_var('cat');

before the conditional code.

share|improve this answer
BRILLIANT!! Thanks a ton, I was trying to use elseifs and so on. Thanks!! – Sledge81 Jun 26 '11 at 13:35
glad y could help. – Bainternet Jun 26 '11 at 13:37
Opps, sorry. Was kinda quick to jump the gun. With the above code even posts in cat 1 are being displayed openly. – Sledge81 Jun 26 '11 at 13:48
Looking at this code, I think in the last instance of else, probably an if statement should come in as well? if its not the category and ignore user status, then show content ?? – Sledge81 Jun 26 '11 at 14:12
$cat in not automatically defined if its set in the url you need to define it yourself, i updated my answer. – Bainternet Jun 26 '11 at 14:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.