WordPress 中文文档
Function Reference/get the category
出自WordPress Chinese 中文文档
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.
Usage
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 ttthe_category()/tt, but without linking each category to the category view, and using spaces instead of commas):
pre?php foreach((get_the_category()) as $category) {
echo $category-cat_name . ' ';
} ?/pre
Examples
Show Category Images
This outputs category images named after the ttcat_ID/tt with the ttalt/tt attribute set to ttcat_name/tt. You can also use any of the other member variables instead.
pre?php foreach((get_the_category()) as $category) {
echo 'img src=http://example.com/images/' . $category-cat_ID . '.jpg alt=' . $category-cat_name . ' /';
} ? /pre
Show the First Category Name Only
pre?php $category = get_the_category(); echo $category[0]-cat_name; ?/pre
(Echoes the first array ([0]) of $category.)
Get the Post Categories From Outside the Loop
pre?php global $post; $categories = get_the_category($post-ID); var_dump($categories); ?/pre
Member Variables
- cat_ID
- the category id (also stored as 'term_id')
- cat_name
- the category name (also stored as 'name')
- category_nicename
- a slug generated from the category name (also stored as 'slug')
- category_description
- the category description (also stored as 'description')
- category_parent
- the category id of the current category's parent. '0' for no parents. (also stored as 'parent')
- category_count
- the number of uses of this category (also stored as 'count')
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 已侦测回归模板: Template:Message /pre
Result
已侦测回归模板: Template:Message
/noinclude

