WordPress.org

Ready to get started?Download WordPress

Forums

Private Category (members only) (3 posts)

  1. CreativeWP
    Member
    Posted 1 year ago #

    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,

  2. supportie
    Member
    Posted 1 year ago #

    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.

  3. CreativeWP
    Member
    Posted 1 year ago #

    Hi Supportie,

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

    Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic