How to output parent and child categories in WordPress

If you have a category tree in your site and often you might be needing to output both child and parents in your category page template (ex: category.php or archive.php). It can be tweaked to work like as breadcrumbs.

I will show you a very easy way on how to output child and parent categories with just one line of code the wordpress has. Further this code can be also placed outside the post loop.

echo(get_category_parents($cat, TRUE, ' » '));
echo(get_category_parents($cat, TRUE, ' '));
echo(get_category_parents($cat, FALSE, ' , '));

This will output

Internet » Blogging » WordPress »
Internet Blogging WordPress
Internet , Blogging , WordPress ,

depending on the current category name.

You can also change the separator instead of raquo you can use a blank space or commas to format the output. If you specify false, it will not print links.