When you view a category in WordPress, all of that category’s subcategories will be included. So if you have a “sports” category with “football” and “baseball” under it, viewing “sports” will show all posts in “sports,” “football” and “baseball.”
If this isn’t the behavior you desire, it is really simple to change it. It doesn’t even require adding any code… only commenting out 3 lines.
The file you modify is different depending on your version of WordPress, and the line numbers are different, but otherwise you’re doing the exact same thing.
WordPress 1.2 and 1.2.1
File to edit: /wp-blog-header.php
Lines 265, 268 and 292 all begin with $whichcat .= get_category_children(
Place two forward slashes in front of those lines: // $whichcat .= get_category_children(
WordPress 1.3
File to edit: /wp-includes/classes.php
Lines 362, 365 and 389 all begin with $whichcat .= get_category_children(
Place two forward slashes in front of those lines: // $whichcat .= get_category_children(
Note: The WordPress 1.3 line numbers are subject to change, so it’s better just to search for $whichcat .= get_category_children(
. You are commenting out all three instances of that code, so it doesn’t matter what line number it is.
WordPress 2.0.2
File to edit: /wp-includes/classes.php
Lines 472, 475 and 513 all begin with $whichcat .= get_category_children(
Place two forward slashes in front of those lines: // $whichcat .= get_category_children(
(thanks to Cordac for this update)
cordac says
Great thing man.. using it!!
Bill says
Thank you for this. Exactly what I needed.
e-head says
Yes … I’m using this too … thanks.
Check out that guys photo above me !
He looks like a gangster from Pulp Fiction …
Scott R. Godin says
As mentioned by cordac in #wordpress, WP 2.0.x lines are 472, 475 and 513 respectively. You may want to add it to your article for those silly people who don’t read the comments. 🙂
Thanks again!
Fundraiser says
A few questions from a blog idiot
How do you keep the spammers from eating you alive? i’ve seen blogs with nothing but spam postings.
How do you keep some left wing extremist from posting racist or defamatory rhetoric? and if you cant stop them, what are you legally liabel when they do?
can viruses be posted to blogs?
pmv says
In version 2.2.2 you’ll have to edit file /wp-includes/query.php and comment out line 885:
$whichcat .= get_category_children($q[‘cat’], ”, ‘, ‘);
Richard says
PLEASE! any update as to how to do this on 2.5?
Rejean says
Thanks for the pointing the solution!
For WordPress 2.7 you will have to edit the file /wp-includes/query.php, this time you have to comment out line 1846:
$in_cats = array_merge($in_cats, get_term_children($q[‘cat’], ‘category’));
I normally would never resort to change the wordpress code, but after several hours of research and failed attempts to work-around the issue, i conclude that this behavior of wordpress is baked-in, and there’s really no way to get around it. An option for the depth would be much welcomed. In fact, i start to find the API of wordpress to seriously lack consistency, since other calls to get the categories do provide such a depth option. sigh..
One must remember it’s free..