WordPress:Template Tags/get the category
Description
描述
Returns an array of objects, one object for each category assigned to the post. This tag must be used within WordPress:The Loop.
返回一组对象,文章的每个类别分配一个。必须在The Loop内使用这个标签。
Usage
用法
This function does not display anything; you should access the objects and then echo or otherwise use the desired member variables.
这个函数不显示任何内容;你应该访问objects并且echo或者使用想要的成员变数。
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):
下面的例子显示了文章的每个类别的名称(就如使用the_category(),但是没有将每个类别链接到类别浏览,而且使用了空格而不是逗号):
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
Examples
例子
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.
产生了类别图像,按cat_ID命名,并且将alt属性设置为cat_name。你也可以使用其它的member变数,取而代之。
<?php foreach((get_the_category()) as $category) { echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />'; } ?>
<?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; ?>
<?php $category = get_the_category(); echo $category[0]->cat_name; ?>
(Echoes the first array ([0]) of $category.)
(Echoes the first array ([0]) of $category.)
Member Variables
Member 变数
- 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')
- cat_ID
- 类别id (也保存为 'term_id')
- cat_name
- 类别名称 (也保存为 '名称')
- category_nicename
- 类别名称产生的一个slug (也保存为'slug')
- category_描述
- 类别描述 (也保存为 '描述')
- category_parent
- 当前类别的母类比的id。'0' 表示没有母类别。 (同样保存为 '母')
- category_count
- 这个类别有多少用途 (也保存为 '数目')
Related
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,
相关的
Description
描述
Displays a link to the category or categories a post belongs to. This tag must be used within WordPress:The Loop.
显示文章所属的类别的连接。必须在The Loop内,使用这个标签。
Usage
用法
%%% <?php the_category('separator', 'parents' ); ?> %%% %%% <?php the_category('separator', 'parents' ); ?> %%%
Examples
Separated by Space
例子
由空格分开
This usage lists categories with a space as the separator.
这个用法用分隔符,隔开类别,列出。
<p>Categories: <?php the_category(' '); ?></p>
<p>Categories: <?php the_category(' '); ?></p>
Separated by Comma
用逗号分开
Displays links to categories, each category separated by a comma (if more than one).
显示类别连接,(如果多于一个类别)类别之间用逗号分开。
<p>This post is in: <?php the_category(', '); ?></p>
<p>This post is in: <?php the_category(', '); ?></p>
Separated by Arrow
用箭头分开
Displays links to categories with an arrow (>) separating the categories. (Note: Take care when using this, since some viewers may interpret a category following a > as a subcategory of the one preceding it.)
用箭头显示类别链接(>)分开类别。(注: 使用这种方法的时候,应该注意,有的访客认为类别后面的> 是前面类别的子类别。)
<p>Categories: <?php the_category(' > '); ?></p>
<p>Categories: <?php the_category(' > '); ?></p>
Separated by a Bullet
用Bullet分开
Displays links to categories with a bullet (•) separating the categories.
使用bullet (•)分开类别,显示类别链接
%%%
Post Categories: <?php the_category(' • '); ?>
%%% %%%
文章类别: <?php the_category(' • '); ?>
%%%
Parameters
参数
- separator
- (string) Text or character to display between each category link. The default is to place the links in an unordered list.
;分隔符: (string)链接类别之间显示的文本或者字符。默认是将链接放在无序列表上。
- parents
- (string) How to display links that reside in child (sub) categories. Options are:
- parents
- (string)怎样显示子类别中的链接。选项有:
- 'multiple' - Display separate links to parent and child categories, exhibiting "parent/child" relationship.
- 'multiple' -分开显示母类别和子类别的链接,显示为"母/子"关系。
- 'single' - Display link to child category only, with link text exhibiting "parent/child" relationship.
- 'single' -只显示子类别链接,链接文本显示为"母/子"关系。
- Note: Default is a link to the child category, with no relationship exhibited.
:注:默认是链接到子类别的一个链接,不显示关系。
Related
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,