WordPress:Template Tags/get the tags
Description
描述
Returns an array of objects, one object for each tag 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 tag name of each tag assigned to the post (this is like using the_tags(), but without linking each tag to the tag view, and using spaces instead of commas):
下面的例子显示了递交给文章的每个标签的名称(这就如使用the_tags(),但是没有将每个标签链接到标签浏览,而且使用了空格,而不是逗号);
<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo $tag->name . ' '; } } ?>
<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo $tag->name . ' '; } } ?>
Examples
例子
Show tag Images
显示标签图像
This outputs tag images named after the term_id with the alt attribute set to name. You can also use any of the other member variables instead.
这使得标签图像以term_id命名 并且将alt属性设置为名称。你也可以使用其它的成员变数替代。
<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo '<img src="http://example.com/images/' . $tag->term_id . '.jpg" alt="' . $tag->name . '" />'; } } ?>
<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo '<img src="http://example.com/images/' . $tag->term_id . '.jpg" alt="' . $tag->name . '" />'; } } ?>
Show the First tag Name Only
只显示第一个标签的名称
<?php $tag = get_the_tags(); if ($tag) { $tag = $tag[0]; echo $tag->name; } ?>
<?php $tag = get_the_tags(); if ($tag) { $tag = $tag[0]; echo $tag->name; } ?>
Member Variables
Member变数
- term_id
- the tag id
- name
- the tag name
- slug
- a slug generated from the tag name
- term_group
- the group of the tag, if any
- taxonomy
- should always be 'post_tag' for this case
- description
- the tag description
- count
- number of uses of this tag, total
- term_id
- 标签id
- 名称
- 标签名
- slug
- 有标签名产生的slug
- term_group
- 如果存在的话,是一组标签
- taxonomy
- 在这种情况下,应该总是 'post_tag'
- 描述
- 标签描述
- 数目
- 这个标签总共的使用次数