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








I’ve been looking for something like this for a while. Thanks! Just one question, what exactly do you mean by within the Loop? Where exactly do I put this? Thanks in advance.
nando: The Loop usually refers to the
< ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
More info: http://codex.wordpress.org/The_Loop
Is it also possible to modify this code so it will only show the child category?
try the following
< ?php
$childcategory ="";
foreach((get_the_category()) as $category) {
if ($category->category_parent != 0) {
$childcategory .= ", ";
}
}
echo substr($childcategory,0,-2);
?>
Thanks for the fast reply!
Unfortunately the above code doesnt show anything…
I will give you some more information about the intended use:
I have made a travel blog with stories about different countries I`ve been to. For every country I have made a category, which includes all the posts about this country.
In every category there is a single post with an introduction to the category. This post is also added to the `frontpage` category (index.php is set to display only posts from `frontpage`)
Now i want to display a link to the child category, so the frontpage shows the introduction, followed by a link to all the posts in this category.
Hope you can help me with this!
Looks like some lines of code got eaten up in the post & comment.
Copy the code from the post and change
if ($category->category_parent == 0) {
to
if ($category->category_parent != 0) {
Unfortuntately this only displays a “,”
I have been thinking about the issue, it is probably easier to modify the frontpage so it will only display posts which are made “sticky”, by using the WP-Sticky plugin.
Any idea how I can do this?
For that, you have to check out the plugin’s site
i had try that but only show one last query category
‘category_parent == 0) {
$parentscategory .= ‘ cat_ID) . ‘” title=”‘ . $category->name . ‘”>’ . $category->name . ‘, ‘;
echo substr($parentscategory,0,-2);
}
}
?>’
Hmm.. am I the only one who got this to work?
I still haven`t solved the issue, so if you have any other ideas they are highly appreciated!