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?

» Bookmark and Share
» Subscribe to Feed
» Leave a Comment

Related Posts:
» WordPress 2.7 Theme Changes
» Updated Firefox Addon: Show Missing Images
» WP Plugin: Genki Announcement

» Newer Post:
« Previous Post: