Okay, so I have a WordPress template that I created that only displays posts that have the “workout” category associated with it. Within the loop that displays those, I want the categories of the specific post to be listed.
I was thinking something like this would work:
$id = get_the_ID();
$cats = wp_get_post_categories($id);
But then I do not know how to echo this out on to the screen.
Anyone have any idea how I can display the categories of each post within the loop? All of the articles I have looked at have only showed how to display all categories, not display the categories associated with a specific post.
Here is the loop I have:
<div class="query">
<b><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></b>
<?php
$id = get_the_ID();
$cats = wp_get_post_categories($id);
?>
</div>
<?php endwhile; ?>
Get the category objects:
Just echo the name:
If you want to output a link, use this:
Note: instead of
wp_get_post_categories($id), you could just useget_the_category().Update: if you want to display all the categories, just loop through them: