【问题标题】:Output tag array for current post当前帖子的输出标签数组
【发布时间】:2015-02-11 20:19:05
【问题描述】:

我正在尝试输出当前帖子的标签(作为数组)。

我将当前帖子的 ID 存储在 $post_id 中,例如 18

我尝试使用以下内容获取$tags 中的帖子标签:

$tags = get_tags($post_id);

但是,当我输出这个(使用 var_dump)时,我发现它输出了博客上存在的 所有 标签,而不仅仅是当前帖子。

例如

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 是与此帖子关联的唯一标签。

我哪里错了?

(我的下一步将是在标签数组中搜索特定术语,例如针织或缝纫,因此我可以根据标签显示不同的图标)

【问题讨论】:

    标签: wordpress tags


    【解决方案1】:

    任何有兴趣的人的答案是使用 get_the_tags 代替,也带有帖子 ID:

    $tags = get_the_tags($post_id);
    

    这给出了一个仅包含此帖子标签的数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多