Question

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!

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.

Was it helpful?

Solution

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>";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top