Parent category: Wordpress
Child category: —- Wordpress Themes
Child category: —- Wordpress Plugins
Child category: —- Wordpress Tips

When you use the_category() template function, it displays both the parent category and any child/subcategories under it. With the above example you will see something like “Filed in Wordpress, Wordpress Themes, Wordpress Plugins, Wordpress Tips”.

Sometimes you prefer less clutter and hide all the child categories. Unfortunately the_category() does not have any optional parameters like child=0 or depth=-1. The below code may be a few lines long but does the job well. To be used within The Loop.


$parentscategory ="";
foreach((get_the_category()) as $category) {
if ($category->category_parent == 0) {
$parentscategory .= ' <a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a>, ';
}
}
echo substr($parentscategory,0,-2);

What's Next?

» Share This (Social Bookmarks/Email)
» Subscribe to Feed
» Subscribe to Email
» Leave a Comment

Related Posts:
» Firefox Addon: Show Missing Images
» WP Plugin: Genki Announcement
» WP Plugin: Genki Pre-Publish Reminder

» Newer Post: Theme Bugfix: TechnoHolic, SoftwareHolic, GenkiTheme (Fixed Width)
« Previous Post: Redesigning Theme for BlogShopr