Categories hacks for WordPress Themes

When customizing a WordPress themes, you need to deal with categories and usually you need to do something like: display category name, display category description or use a category image.

Here are a few category hack snippets that you can use in your own themes:

Display category name

This trick allows you to display category name.

$category = get_the_category();
echo $category[0]->cat_name;

Display category description

This trick shows how to display the category description. Please notice here you need to specify category_description instead of cat_name. In order to see any effect when you use this, do not forget to edit your category description under wp-admin, otherwise you won’t see anything.

$category = get_the_category();
echo $category[0]->category_description;

Display other category information

This trick shows you how to display other category information like parent category, category slug (nicename) or count how many post are inside a given category.

$category = get_the_category();
echo $category[0]->category_parent;
echo $category[0]->category_nicename;
echo $category[0]->category_count;

Advertisement

Did you like it?

No comments yet.

Leave a Comment