Function Reference/get the category

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

Description

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.

Usage

<?php get_the_category( $id ) ?>

Parameters

ttid/tt
(integer) (optional) The post id.
Default: $post->ID (The current post ID)noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Examples

Show All Categories as Links

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);
}
?>

Show Category Images

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 . '" />'; 
} 
?>

Show the First Category Name Only

<?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>';
}
?>

Get the Post Categories From Outside the Loop

<?php
global $post;
$categories = get_the_category($post->ID);
var_dump($categories);
?>

Return Values

(array) 
Array of Category Objects

Member Variables in Returned Objects

term_id 
string '1'
name 
string 'Cat Name'
slug 
string 'cat-slug'
term_group 
string '0'
term_taxonomy_id 
string '1'
taxonomy 
string 'category'
description 
string
parent 
string '0'
count 
string '1'
object_id 
string '1'
cat_ID 
string '1' - the category id (also stored as 'term_id')
category_count 
string '1' - the number of uses of this category (also stored as 'count')
category_description 
string 'description' - the category description (also stored as 'description')
cat_name 
string 'Cat Name' - the category name (also stored as 'name')
category_nicename 
string 'cat-slug' - a slug generated from the category name (also stored as 'slug')
category_parent 
string '0' - the category id of the current category's parent. '0' for no parents. (also stored as 'parent')

Changelog

Source Code

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn

Related

the_category, the_category_rss, single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, get_the_category, get_category_parents, get_category_link, is_category, in_category

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude