【发布时间】:2019-10-30 15:12:48
【问题描述】:
我必须从自定义标签列表中检索所有不重复的标签,所以我遵循了以下给出的解决方案:
How to remove ducplicate tags from custom tag list?
我尝试自定义解决方案中给出的PHP代码如下,只得到foreach块返回的一个标签ID:
if(is_category()):
$category = get_query_var('cat');
$categories = get_category($category);
endif;
$tagIDs = array();
query_posts('category_name='.$categories->slug);
if(have_posts()) : while(have_posts()) : the_post();
$tags = get_the_tags();
if($tags):
foreach($tags as $tag){
if(!in_array($tag->term_id, $tagIDs)):
$tagIDs[] = $tag->term_id;
$tagNames[$tag->term_id] = $tag->name;
endif;
}
endif;
endwhile; endif;
wp_reset_postdata();
echo '<ul>';
foreach($tagIDs as $tagID):
echo '<li><a href="'.get_tag_link($tagID).'">'.$tagNames[$tagID].'</a></li>';
endforeach;
echo '</ul>';
我必须检索所有没有重复的标签 slug。我怎样才能做到这一点?
【问题讨论】:
-
那么,这些代码有没有返回任何东西?
$tagIDs是否包含所有标签 ID? -
$tagID 只返回一个 ID,即 9。
标签: php wordpress tags custom-tags