Codex tools: Log in / create account
Contents |
Returns an array of objects, one object for each category assigned to the post. This tag must be used within The Loop.
This function does not display anything; you should access the objects and then echo or otherwise use the desired member variables.
The following example displays the category name of each category assigned to the post (this is like using the_category(), but without linking each category to the category view, and using spaces instead of commas):
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
This outputs category images named after the cat_ID with the alt attribute set to cat_name. You can also use any of the other member variables instead.
<?php
foreach((get_the_category()) as $category) {
echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}
?>
<?php $category = get_the_category(); echo $category[0]->cat_name; ?>
(Echoes the first array ([0]) of $category.)
the_category, the_category_rss, single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, in_category, get_category_parents, get_the_category get_category_link,