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?
Use get_the_category() like this: <? php foreach((get_the_category()) as $category) { echo $category->cat_name .
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With