0

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.

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.