Lonewolf Online

The website of an amateur photographer, astronomer and petrolhead.

Prevent Showing of Sub Category Posts

In some cases you may wish to display ONLY the posts of a category and not posts of its child categories on a Wordpress blog.

Lets say you had a parent category of “Weather” with sub-categories “America” and “United Kingdom”. When a user clicks on the weather category, Wordpress will automatically display posts from the Weather category AS WELL AS the posts of America and United Kingdom.

If this is not the behaviour you are after, there is a template tag trick that you can use in your theme to show posts of the current category ONLY.


If you have an archive.php or category.php open that, otherwise open index.php. Don’t forget to back them up just in case things go wrong!

Find the line similar to:

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

Which is the start of "The Loop" and add after it on the next line:

<? if ((is_category()) && in_category($wp_query->get_queried_object_id())) { ?>

This line checks if we are viewing a category, and that the post being processed is a member of the category being viewed. If it is then we will show the post.

$wp_query->get_queried_object_id() returns an integer ID of the category being viewed.

in_cagegory() tests if the current post in the loop is a member of the specified category, in this case we pass it the current category.

Next find the line:

<?php endwhile; ?>

Which is the end of the loop, and add a line BEFORE it containing:

<?php } ?>

This small modification will stop posts of child categories from showing when a parent category is selected. Of course you will need some posts in Weather otherwise you will have a blank page. You can also use the information on the Wordpress Codex template tags to create static pages for each category if you wish.

If you only wish to stop sub category posts from showing on one particular parent category you can specify the required category in the is_category() method. Simply insert the numerical id of the parent category in the brackets, e.g. is_category(10) will stop sub category posts from showing only if we are viewing the category with ID of 10. All other categories will retain their default setting.

 

Share/Save/Bookmark

 

Have Your Say

 

 

 

 

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment. The authors reserve the right not to publish any comments that they believe are hateful, racist, demeaning or otherwise inappropriate.

 

What Others Are Saying

Andy Posted on Thursday 17th July 2008 at 2:24pm Comment

Yep, didn&squot;t work for me either…

Richard Posted on Wednesday 7th May 2008 at 3:03pm Comment

causes a fatal error in category.php in V 2.51 Any ideas??

thanks

Everett Posted on Monday 17th March 2008 at 9:19pm Comment

I tried this fix but then the first page of every main category was blank. The &quot;next&quot; page would show entries that were inside that main category, but for some reason the first page still showed up without any posts on it. ???

Click to Add Your Comments

 

  • Random Photographs
    • Firework Night 2006
    • Sea Kings Deploy Royal Marine Commandos
    • Coxs Cave
    • DisneyLand Paris
    • View back to Village
    • Ernie, A Tiny Kitten
    • Eiffel Tower
    • Bristol
    • Goughs Cave
    • Chinook Transporter

  • Recent Additions
  • Search



  • Page copy protected against web site content infringement by Copyscape
This page was last updated on Wednesday, July 30th, 2008.
Unless otherwise stated, all photographs and content Copyright © 2000 - 2008 Tim Trott, All Rights Reserved. No graphics, photographs or content may be used without written permission.
Website Design and Graphics Copyright © 2005-2007 Tim Trott. home :: sitemap :: disclaimer:: contact