Codex tools: Log in
Languages: English • 日本語 • (Add your language)
Contents |
Returns an array of objects, one object for each category assigned to the post. This tag may be used outside The Loop by passing a post id as the parameter.
This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms.
<?php get_the_category( $id ) ?>
This outputs all the categories assigned to the post as links. Must be used inside the loop. You can also use the function get_the_category_list() for this.
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
}
echo trim($output, $separator);
}
?>
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.)
Make the first category link to the category page.
<?php
$category = get_the_category();
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>
<?php global $post; $categories = get_the_category($post->ID); var_dump($categories); ?>
wp-includes/category-template.php
Categories: the_category(), the_category_rss(), single_cat_title(), category_description(), wp_dropdown_categories(), wp_list_categories(), get_the_category(), get_the_category_by_ID(), get_category_by_slug(), get_the_category_list(), get_category_parents(), get_category_link(), is_category(), in_category()