我很高兴你弄明白了,但下面是一个快速的语义更改(替换使用$tag
单数和$tags
复数将帮助它更接近于它所做的事情;在功能上也没有什么不同)。
我还添加了循环内外的情况,以及一个条件,这样在没有标记的情况下就不会尝试foreach循环。
评论:
//We are inside the Loop here. Other wise we must pass an ID to get_the_tags()
//returns array of objects
$tags = get_the_tags();
//make sure we have some
if ($tags) {
//foreach entry in array of tags, access the tag object
foreach( $tags as $tag ) {
//echo the name field from the tag object
echo $tag->name;
}
}
内部回路
$tags = get_the_tags();
if ($tags) {
foreach( $tags as $tag ) {
echo $tag->name;
}
}
外回路:
$id = \'10\';
$tags = get_the_tags( $id );
if ($tags) {
foreach( $tags as $tag ) {
echo $tag->name;
}
}
内部进一步
the loop,
get_the_tags()
使用当前的post id。在循环外部,需要向其传递id。无论哪种方式,它都会返回一个
WP_TERM
对象与相关职位相关的每个术语。
[0] => WP_Term Object
(
[term_id] =>
[name] =>
[slug] =>
[term_group] =>
[term_taxonomy_id] =>
[taxonomy] => post_tag
[description] =>
[parent] =>
[count] =>
)
您可以使用与上述相同的方式访问每个值,即:
$tags = get_the_tags();
if ($tags) {
foreach( $tags as $tag ) {
echo $tag->term_id;
echo $tag->name;
echo $tag->slug;
echo $tag->term_group;
echo $tag->term_taxonomy_id;
echo $tag->taxonomy;
echo $tag->description;
echo $tag->parent;
echo $tag->count;
}
}