> WordPress中文手册 > wordpress标签常用函数:get_the_tags()

【函数介绍】

返回一组对象,每个递交给文章的标签分配一个对象。必须在The Loop内使用这个标签。

【函数用法】

这个标签不显示任何内容;你应该访问objects,然后echo或者使用想要的变数。

下面的例子显示了递交给文章的每个标签的名称(这就如使用the_tags(),但是没有将每个标签链接到标签浏览,而且使用了空格,而不是逗号);

<?PHP
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' '; 
}
}
?>

【参数】

term_id
标签id
name
标签名
slug
有标签名产生的slug
term_group
如果存在的话,是一组标签
taxonomy
在这种情况下,应该总是 ‘post_tag’
description
标签描述
count
这个标签总共的使用次数

【示例】

显示标签图像
这使得标签图像以term_id命名 并且将alt属性设置为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 . '" />'; 
}
}
?>

只显示第一个标签的名称

<?php
$tag = get_the_tags(); 
if ($tag) {
$tag = $tag[0]; echo $tag->name;
}
?>