如何从自定义标签列表中删除重复标签? [英] How to remove ducplicate tags from custom tag list?
问题描述
我有这段代码来显示当前类别中使用的标签,包括子类别:
I have this code to display the tags which are used in the current category, includes child categories:
if (is_category( )) {
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
query_posts('category_name='.$yourcat->slug.'');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
是否可以删除重复的标签?现在,该代码多次显示某些标签,而不是一次。
Is it possible to remove duplicate tags? Right now the code shows some tags multiple times instead of once.
关于此代码的另一个问题:如何输出这样的每个标签以创建复选框表单?
Another question about this code: How can I output each tag like this to create a checkbox form?
<input type="radio" name="tag" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>> Tag1<br>
希望有人可以帮助我。
Hopefully someone can help me with this. Thanks in advance!
更新:
我忘了提到我使用代码 if(( isset($ _ GET [ tag]))&& $ _GET [ tag] == tag1){在输入字段的末尾回显 checked;}
选中使用标记时的复选框,但是如果我使用该代码(可在html中使用),则页面无法正确显示。
Update:
I forgot to mention that I use the code if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}
at the end of the input field to check the checkbox when the tag is used, but if I use that code (which works in html) the page don't display properly.
推荐答案
我已经稍微修改了您的代码,但是现在您有了一个数组,可以用于任何目的,例如ex。
I've modified your code a bit, but now you have an array of tag id's which you can use for any purpose, for ex. list added below.
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name='.$yourcat->slug);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
echo "<ul>";
foreach($tag_IDs as $tag_ID){
echo '<a href="'.get_tag_link($tag_ID).'">'.$tag_names[$tag_ID].'</a>';
}
echo "</ul>";
这篇关于如何从自定义标签列表中删除重复标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!