Support » Fixing WordPress » Private Category (members only)

  • Hi every one, I have a website that has four categories, 2 public and 2 private. Only those log in should be able to see the extra 2 categories. This is the code I found, but I can not get it to work:

    <?php if(in_category( array( 5,10 ) ) { // if the current post is in categories 5 or 10 ?>
        <?php if ( !is_user_logged_in() ) { // check whether the user is logged in ?>
           <p>You'll need to be a member to view this content.</p>
       <?php } else { // user is logged in so go ahead and show content ?>
          <?php the_content(); ?>
      <?php } ?>
    <?php } ?>

    Any ideas? I was told to put that on the head.php, but nothing happens. Any help would be appreciated.

    Thanks,

Viewing 2 replies - 1 through 2 (of 2 total)
  • That code shouldn’t be in header.php. It depends on the theme but generally It will need to go within index.php and single.php. In these files you will see “the loop” which is where your posts are displayed, this is where your code should go.

    So it will look something like this (haven’t tested it)

    <?php while ( have_posts() ) : the_post(); ?>
         <?php if(in_category( array( 5,10 ) ) { // if the current post is in categories 5 or 10 ?>
        <?php if ( !is_user_logged_in() ) { // check whether the user is logged in ?>
           <p>You'll need to be a member to view this content.</p>
       <?php } else { // user is logged in so go ahead and show content ?>
          <?php the_content(); ?>
      <?php } ?>
    <?php } ?>
    <?php endwhile; ?>

    Cheers.

    Hi Supportie,

    When I use this code, it just loops, so the text of the_content keeps repeating, till who knows when.

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Private Category (members only)’ is closed to new replies.