How to display category and tag descriptions in a post

I want to display the category name and description in every post automatically.

I have been trying the method of wpbeginner and searching in other platforms but it's not working for me. I cannot find this function.

For example, if I'm talking about tomatoes (the post), I want to display in it its information about vegetables (the category).

Any idea about how to do it?

Thanks.

Topic description php Wordpress

Category Web


First of all, remember that a post can have multiple categories, so you'll have to handle an array and then print the results.

To get see what categories are attached to your post, print the value from the get_the_category function inside a <pre></pre> (which formats the code):

$categories = get_the_category(); 
echo '<pre>';
var_dump($categories);
echo '</pre>';

This code will print the category name, add a like break and then print the category description.

The var_dump will display the output from the function.

After you understand the output from the function, you can display the categories by using this:

 foreach((get_the_category()) as $category){
    echo $category->name."<br>";
    echo category_description($category);
 }

Feel free to ask more question if this do not solve your problem.

Note: Tomatoes are a fruit :P

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.