In wordpress, sometimes you may have many subcategories for a different parent categories. By default wordpress lists all sub-categories.
if i have category structure like
Drupal
- 2 Column
- 3 Column
- Left sidebar
- Right sidebarWordpress
- Fixed Width
- Widget Ready
- News/Magazine
- Photogallery
and if i do a wordpress post (with title say CSS Gallery) inside Wordpress -> Photogallery i want the title to display
Blog Title > Parent Category Name > Blogpost Title
like
Top Best Themes > Wordpress > CSS Gallery
Here is the wordpress code to display parent category name. Its a very simple code but did the job for me. You can insert inside header.php if you want to change titles in wordpress. This is how it works. First it gets the category name of wordpress post, and from it gets the Category ID of its parent then it gets the parent name with get_cat_name() function. If the parent is empty (which means the wordpress post is already listed in the parent category) then print the current category name.
<?php
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo '» ' . $parent;
} else {
echo '» ' . $category[0]->cat_name;
}
?>
Note that this code has a limitation and it works best with only one level deep.!
Similar Posts:
- Wordpress problem with subcategories under identical names!
- 3 Must Do Things for Wordpress Blog Starters
- Tutorial: How to write a Wordpress Plugin?
- Changing Quote and Code Tags in Wordpress
- How to change vBulletin Homepage Title?
- Best & Useful Wordpress Plugins For Blogs
- How to post source code in Wordpress posts?
- Displaying HTML code in Wordpress Posts!
- 10 Best Wordpress PLUGINS You Never want to MISS!
- Display only top level parent categories in wordpress?


March 15, 2009
How about getting the link of the parent category? Thanks.
May 27, 2009
Thanks for the code snippet — super simple and straight to the point.
May 30, 2009
hi pbu
thanks for the code, i was searching via google and found many ways, but yours the only one that worked.
thanks
June 10, 2009
I appreciate your hard work in writing these posts. I know how difficult it can be to keep the ideas flowing. Thanks!
June 17, 2009
Your blog is amazing, i first landed to another post but then get interested and thought, i will just look a little more arround to see what else i can find out about such stuff
November 9, 2009
Thanks for your code! You should remove the second “empty” after !empty, at it will cause an error
Cheers,
Sven
January 12, 2010
I love u man, u code is amazing and perfect ^^, thanks thanks