How to Exclude a Blog Category from Displaying

Have you ever wanted to exclude a particular category from a section of your blog without using a plugin?

For example, because there is already a separate list of videos on the homepage at RosalindGardner.com, I didn’t want the videos to show up again in the “Most recent entry posted” featured at the top of the page.

So, to eliminate the Videos category from that section, I first look go to Manage > Categories in my Wordpress Administration panel and look for the category ID, which turns out to be #10.

I open the home.php template (you might be using index.php) and add cat=-10 to the code for the most recent post as follows:

< ?php $recent = new WP_Query("showposts=1&cat=-10"); while($recent->have_posts()) : $recent->the_post();?>

Note the minus sign in front of the category number.

If you want to eliminate more than one category from the display, simply separate each category ID number with a comma, such as: &cat=-10,-22 .

Popularity: 3% [?]

If you're new here, you may want to subscribe to my RSS feed or the No-Hype, No-BS, No Spam NPT newsletter. Thanks for visiting!

Comments

One Response to “How to Exclude a Blog Category from Displaying”

Michael Chalke on March 15th, 2008 9:19 am

Dear Rosalind, First I hope yopu are on the mend and getting over your traumatic surgery. Good Luck.xxx
Second is that I have liked all the good things coming my way from you, but am surprised to see and unfortunately buy (£39.99) the programme on your blogsite.
Namely “How I earn £14,246 a week”. This is a con for hard up pensioners like me. It’s a Gambling programme primarily, and betting against a Roulette Wheel and Football results. A Game of chance which I am sure will loose a lot of money if you followed it. I think he is also reselling with no Rights. I have asked for a refund.
Thought you might like to know and perhaps check him out.
Regards Michael Chalke.

[Reply]

MyAvatars 0.2

Got something to say?