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!
- How to output parent and child categories in WordPress
- How to output category name in WordPress?
- How to show adsense on specific posts/pages in WordPress?
- WordPress – How to get category ID for use in category.php
- 3 Must Do Things for WordPress Blog Starters
- How to get image attachment ID in WordPress?
- GD star rating issue replacing default 0 valueg
- How to get custom field value in WordPress?
- 20+ Beautiful Photo Slideshow Plugins for 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
March 17, 2010
First i got error with your code but after editing small thing, it’s work… thank you… so much… love it.
April 6, 2010
Thanks, just what I needed!
April 23, 2010
Great site, great info…keep it coming
May 20, 2010
RSS Feeds are really very helpful and you could get site and news updates from it.-;~
June 2, 2010
Hi pbu,
That helped me, thank you very much
July 28, 2010
RSS feeds are really great because you are always updated with the latest news or blog posts.*:;
October 25, 2010
RSS feeds are necessary for transmitting your blog updates to your readers or followers“”
November 10, 2010
wordpress is simply the best blogging platfrom that you can use, it has lots of features ,.-
December 10, 2010
If anyone is looking for code to get parent categories but also have the link, check out this site: http://wordpress.org/support/topic/creating-the-parent-category-as-a-link-when-using-the_category
Code at the very bottom worked perfect for me.
July 16, 2011
thank you so much. you can’t imagine how long it took me to find a code like this. I just changed it a tiny bit to eliminate the >> and add the link to the parent category. THANK YOU. x
October 10, 2011
can somebody help me where to find my source code in wordpress? how to i edit to put some adsense and to customize more.. i found it hard.
anybody help me?
October 13, 2011
Thank you!