About design and nearby

How to Remove Specific Categories from the Loop

Posted: May 19th, 2014 | Author: | Filed under: Development, Tips and Tricks | Tags: , , | No Comments »

Once working with WordPress I wish to exclude all posts in category 22 or (‘Careers’) from showing on the home page. A simple solution like

  1. <?php query_posts('cat=-22'); ?>

or

  1. <?php
  2. $exclude = get_cat_ID('careers');
  3. $q = 'cat=-'.$exclude;
  4. query_posts($q); ?>

excludes all posts of category 22 (‘Careers’) from my homepage, but breaks my pagination; when the ‘Newer Entries’ or ‘Older Entries’ buttons are pressed, it simply returns the same posts.

  1. <?php if ( is_home() ) {
  2. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  3. query_posts($query_string . '&cat=-22&paged='.$paged);
  4. } ?>

I placed the code above before the main loop:

  1. <?php if (have_posts()) : ?>
  2. <?php while (have_posts()) : the_post(); ?>
  3.   your code here
  4.  <?php the_content(); ?>
  5.  
  6. <?php endwhile; ?>

on the index.php page – works as a charm!

You could be interested to read related post: How to Show or Exclude Posts from a Single Category only

Share Button

Check Related Posts:


Leave a Reply


  • eight × = 72

Looking for a Freelance UX & UI designer for your project? I’m ready to jump onboard!

Let's discuss your project now