Since WordPress doesn’t allow for limiting the category in “the_category” I had to write some custom code to achieve limiting the categories displayed for the current post to the current category and it’s parent.
<?php //display the current and parent category (2 total)
$category = get_the_category();
$current_category = $category[0];
$parent_category = $current_category->category_parent;
if ( $parent_category != 0 ) {
echo '<a href="' . get_category_link($parent_category) . '">' . get_cat_name($parent_category) . '</a>';
}
echo '<a href="' . get_category_link($current_category) . '">' . $current_category->cat_name . '</a>';
?>
props: Nick



Nice code
Thanks. You did help…which I totally forgot.
Wondering if you can help…
I have 3 categories – one, two and three
On each category page, i want to display the post, plus a link to all the subcategories of “three” that the post is assigned to. I can't seem to do this cause like you said, there is no limit ability that i can find
I don't understand. Do you want to show the subcategories of the current post only?
I have a problem using your code, it shows the Cat title of 1 step back.
For example, current category is 10, the display is the title of category 9.
How can be? How to solve
Not sure, can you post the code on gist.github so I can review it?
Thanks for this code, I’ve been using it to get the post’s parent cat passed on as a class of the post div. I’ve been using it with another function to convert the ID to the category slug, when I encountered the following error:
it needs to read [code]if ( $parent_category !== 0 )[/code] (notice the extra “=” to make sure it really doesn’t use ID 0.
Cheers.
Thanks for this code, I’ve been using it to get the post’s parent cat passed on as a class of the post div. I’ve been using it with another function to convert the ID to the category slug, when I encountered the following error:
it needs to read [code]if ( $parent_category !== 0 )[/code] (notice the extra “=” to make sure it really doesn’t use ID 0.
Cheers.
Thanks I have used it…
Thank you, sir!
I think that what andrew and me wanna do is to show all the parent categories
what this code do is to show the current category and the parent category of the current category only.
What if the parent category was a child category for another parent category??
This code wont show the original parent category..
For example we have category1 which contains category2 which contains category3
What this code does is to show the categories like this (Category2 » Category3)
What if I wanna display it like this (Category1 » Category2 » Category3)
I mean we dont need the just 2 categories limit?
I hope that this what andrew wants to do. If it isnt can you tell me how because I wanna do it