Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the category title in a post in Wordpress?

Tags:

Say I have a post called Hello World in Wordpress and I'm directly viewing this page, how would I go about finding the category of "Hello World" and displaying it?

like image 872
Keith Donegan Avatar asked Jun 03 '09 17:06

Keith Donegan


People also ask

How do I get a post category title in WordPress?

Use get_the_category() like this: <? php foreach((get_the_category()) as $category) { echo $category->cat_name .

How do I find the category ID of a WordPress post?

After the Reveal IDs plugin has been activated, you can navigate to Posts => Categories in your WordPress dashboard and the category IDs will be displayed accordingly.

How do I show categories on post page in WordPress?

In menus, go to Appearance → Menus, select categories and click Add to Menus. In the sidebar, go to Appearance → Widgets, then choose the categories that you want to appear in the sidebar and click Add Widget. When you want to show subcategories in the sidebar, drag and drop categories to a Sidebar.

How do I get a list of category names in WordPress?

The Wordpress function we can use is get_categories(). $categories = get_categories( $args ); This will return an array of category objects where you can loop through and get all the information you need. You can pass in one parameter to this function which you can use to narrow down which categories are returned.


1 Answers

Use get_the_category() like this:

<?php foreach((get_the_category()) as $category) {      echo $category->cat_name . ' ';  }  ?> 

It returns a list because a post can have more than one category.

The documentation also explains how to do this from outside the loop.

like image 180
RichieHindle Avatar answered Oct 20 '22 04:10

RichieHindle