WordPress.org

Ready to get started?Download WordPress

Forums

in_category(category_id) is not working. (4 posts)

  1. planesfillingwc
    Member
    Posted 6 years ago #

    For some reason, no matter what I try, I cannot get in_category(category_id) to work. On my index.php page, I want to display only one category of posts in the loop. I've read as much as I can about the topic, tried numerous things, but nothing works. All that happens is that it continues to display posts from all categories that then bleed into the sidebar and mess up my layout. Any ideas?

    Here's my loop code:

    <div class="content">
      <div class="leftcontent">
    	<?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>
         <?php if (!in_category($il_asides_cat) ) { ?>
    
        <div class="post one" id="post-<?php the_ID(); ?>">
       	<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><?php if ($il_more_authors == "True") { ?> <span class="chrondate"><?php the_author_posts_link('nickname') ?></span><?php } ?></h2>posted on <?php the_date() ?> / <?php the_time() ?> <br />
    <?php the_tags(); ?></p></div>
    
    	   <div class="entry">
          <?php the_content("[more]"); ?>
    	  <p>
    	  <?php comments_popup_link('comment', '1 comment', '% comments'); ?>
    	</div><!-- end entry -->
      </div>		
    
    	   <?php } else { ?>
    
       <div class="post aside" id="post-<?php the_ID(); ?>" style="padding-bottom:0em;">
       	<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> <span class="chrondate"><?php the_author_posts_link('nickname') ?></span></h3>
    	   <div class="entry">
          <?php the_content("[...]"); ?>
    	 </div><!-- end entry -->
      </div>
    
    		 <?php } endwhile; ?>
    </div>
    
    	<?php else : ?>
  2. Ivovic
    Member
    Posted 6 years ago #

    if that's your goal, then you want this:

    http://codex.wordpress.org/Template_Tags/query_posts
    http://codex.wordpress.org/The_Loop

    pay particular attention to 'cat='

  3. planesfillingwc
    Member
    Posted 6 years ago #

    Thanks, you definitely got me heading in the right direction. It seems query_posts('cat=3'); is what I want to be using, but when I drop it in just before The Loop it to display all categories and bleed into the sidebar as before.

    Here is what my updated Loop looks:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php query_posts('cat=3'); ?>
    
        <div class="post one" id="post-<?php the_ID(); ?>">
       	<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><?php if ($il_more_authors == "True") { ?> <span class="chrondate"><?php the_author_posts_link('nickname') ?></span><?php } ?></h2>posted on <?php the_date() ?> / <?php the_time() ?> <br />
    <?php the_tags(); ?></p></div>
    
    	   <div class="entry">
          <?php the_content("[more]"); ?>
    	  <p>
    	  <?php comments_popup_link('comment', '1 comment', '% comments'); ?>
    	</div><!-- end entry -->
      </div>		
    
    <?php endwhile; else: ?>
    	<div class="entry"><h2>not found</h2></div>
    
     <?php endif; ?>

    I've tried <?php if (in_category('5')) continue; ?> in place of <?php query_posts('cat=3'); ?> and it works just fine but only as a temporary solution. It's excluding all posts from category 5 and showing only the news category on my index page. But, the problem is that it only excludes one category so as soon as I start using multiple categories on my site (which intend to), they'll all start being displayed on my index.php except for 5.

  4. taghaboy
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic