Grow your CSS skills. Land your dream job.

Last updated on:

Remove Specific Categories From The Loop

<?php query_posts('cat=-3'); ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

		<h3></h3>
		
		<p><?php the_time('F jS, Y') ?></p>

		<?php the_content(); ?>

<?php endwhile; ?>

The area inside the while loop could be anything, that's just some example typical inside-loop code. The trick is the first line, and the cat=-3 parameter. "-3" in this case is the ID of the category, and you can replace that with any category you wish to remove from the loop. You may use comma separated values here to remove more than one category (e.g. cat=-1,-2).

Comments

  1. I use that so i can have 2 pages that display different posts one display the news and the other my latest work everything works fine but for some reason the read more button does not appear any suggestions thanks

    • Permalink to comment#

      Hi

      Do you know how to code this if you used /work/ instead of an id.

      Thanks

  2. i use it within looping. is it ok?

    • here..

      
       <?php if (have_posts()) : ?>
       <?php query_posts($query_string.'&cat=-70'); while (have_posts()) : the_post(); ?>
      
      - - -  content - - -
      
      <?php endwhile; ?>
      <?php wp_pagenavi(); ?>
      <?php else : ?>
      <?php include(TEMPLATEPATH . '/404.php'); ?>
      <?php endif; ?>
      

      because i’m using wp_pagenavi… is it ok?

  3. Dexter

    Messed with pagination, you guys should check some things before starting giving tips.

  4. W@RL0CK
    Permalink to comment#
                <?php twentyeleven_content_nav( 'nav-above' ); ?>
    
                <?php /* Start the Loop */ ?>
                <?php while ( have_posts()) : the_post(); ?>
    
                    <?php get_template_part( 'content', get_post_format() ); ?>
    
                <?php endwhile; ?>
    
                <?php twentyeleven_content_nav( 'nav-below' ); ?>
    
            <?php else : ?>
    
                <article id="post-0" class="post no-results not-found">
                    <header class="entry-header">
                        <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
                    </header><!-- .entry-header -->
    
                    <div class="entry-content">
                        <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
                        <?php get_search_form(); ?>
                    </div><!-- .entry-content -->
                </article><!-- #post-0 -->
    
            <?php endif; ?>
    

    Something goes wrong with the pagination. As in it hides the category specified but certain non-specified categories don’t show up. Seems, that the solution provided by you guys messes up the pagination code. Please Help !!!

  5. Aaron
    Permalink to comment#

    Uhh, not sure what happened, but I meant to post that even though this post is a little old, people who come across it today might want to take care in using $query_posts() since it can be a very risky function. I’d suggest using WP_Query() instead.

    Here’s a sample from a project I’m working on now (which…uhm, you got a little sample of above…)

            <ol class="posts-list">
    
                <?php // exclude posts in gallery category
                    $catid = get_cat_ID('gallery');
                    $args = array('cat' => '-' . $catid);
                    $the_query = new WP_Query( $args );
    
                    if ( $the_query->have_posts() ) : ?>
                        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
                            <li>
                                <h1 class="entry-title">
                                    <a href="<?php the_permalink(); ?>" title="<?php echo get_the_title(); ?>" rel="bookmark"><?php the_title(); ?></a>
                                </h1>
                                <span class="entry-date"><?php echo get_the_date(); ?></span>
                                <span class="post-tags"><?php echo the_tags("",",",""); ?></span>
                            </li>
    
                    <?php endwhile; ?>
                <?php endif; ?>
    
            </ol>
    
    • Rhys
      Permalink to comment#

      Brilliant, this is exactly what I was looking for. Thank you Aaron.

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".