当前 post 的输出标记数组
Output tag array for current post
我正在尝试输出当前 post 的标签(作为数组)。
我将当前 post 的 ID 存储在 $post_id
中,例如 18
我试图通过以下方法获取 $tags
中的 post 标签:
$tags = get_tags($post_id);
然而,当我输出这个(带有var_dump)时,我发现它输出博客上存在的所有标签,而不仅仅是当前的post.
例如
array (size=3)
0 =>
object(stdClass)[116]
public 'term_id' => string '10' (length=2)
public 'name' => string '2648' (length=4)
public 'slug' => string '2648' (length=4)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '10' (length=2)
public 'taxonomy' => string 'post_tag' (length=8)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '3' (length=1)
1 =>
object(stdClass)[118]
public 'term_id' => string '12' (length=2)
public 'name' => string 'craft fair' (length=10)
public 'slug' => string 'craft-fair' (length=10)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '12' (length=2)
public 'taxonomy' => string 'post_tag' (length=8)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '1' (length=1)
2 =>
object(stdClass)[1731]
public 'term_id' => string '11' (length=2)
public 'name' => string 'knitting' (length=8)
public 'slug' => string 'knitting' (length=8)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '11' (length=2)
public 'taxonomy' => string 'post_tag' (length=8)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '1' (length=1)
只有键 2 =>
应该显示,因为 knitting
是与此 post 关联的唯一标签。
我哪里错了?
(我的下一步是在标签数组中搜索特定术语,例如编织或缝纫,这样我就可以根据标签显示不同的图标)
任何感兴趣的人的答案是使用 get_the_tags
代替,也使用 post ID:
$tags = get_the_tags($post_id);
这给出了一个仅包含此 post 标签的数组。
我正在尝试输出当前 post 的标签(作为数组)。
我将当前 post 的 ID 存储在 $post_id
中,例如 18
我试图通过以下方法获取 $tags
中的 post 标签:
$tags = get_tags($post_id);
然而,当我输出这个(带有var_dump)时,我发现它输出博客上存在的所有标签,而不仅仅是当前的post.
例如
array (size=3)
0 =>
object(stdClass)[116]
public 'term_id' => string '10' (length=2)
public 'name' => string '2648' (length=4)
public 'slug' => string '2648' (length=4)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '10' (length=2)
public 'taxonomy' => string 'post_tag' (length=8)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '3' (length=1)
1 =>
object(stdClass)[118]
public 'term_id' => string '12' (length=2)
public 'name' => string 'craft fair' (length=10)
public 'slug' => string 'craft-fair' (length=10)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '12' (length=2)
public 'taxonomy' => string 'post_tag' (length=8)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '1' (length=1)
2 =>
object(stdClass)[1731]
public 'term_id' => string '11' (length=2)
public 'name' => string 'knitting' (length=8)
public 'slug' => string 'knitting' (length=8)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '11' (length=2)
public 'taxonomy' => string 'post_tag' (length=8)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '1' (length=1)
只有键 2 =>
应该显示,因为 knitting
是与此 post 关联的唯一标签。
我哪里错了?
(我的下一步是在标签数组中搜索特定术语,例如编织或缝纫,这样我就可以根据标签显示不同的图标)
任何感兴趣的人的答案是使用 get_the_tags
代替,也使用 post ID:
$tags = get_the_tags($post_id);
这给出了一个仅包含此 post 标签的数组。