Title: Calling Current Category?
Last modified: August 20, 2016

---

# Calling Current Category?

 *  [tarleton](https://wordpress.org/support/users/tarleton/)
 * (@tarleton)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/calling-current-category/)
 * Hi All,
 * Working on my first wordpress site and have a quick question I’ve got the following
   code:
    (archive.php)
 *     ```
       <?php
       $cat_id = get_query_var('cat');                     query_posts('category_name=$cat_id');                     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();                  ?>
       ```
   
 * What I’m trying to do here is to call the tags of the current category.
 * EG: [http://mysite.com/?cat=10](http://mysite.com/?cat=10)
    I will get a list
   of tags used in posts in the category with the id of 10.
 * I’m sure I’m just making a stupid error here any help appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/calling-current-category/#post-2137279)
 * this might work:
 *     ```
       $cat_id = get_query_var('cat');
       query_posts('posts_per_page=-1&cat='.$cat_id);
       if (have_posts()) :
       echo '<ul>';
       while (have_posts()) : the_post();
       if( get_the_tag_list() ){
       echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
       }
       endwhile;
       echo '</ul>';
       endif; wp_reset_query(); ?>
       ```
   
 * however, you might get a lot of duplicate tags.
 * query parameters:
    [http://codex.wordpress.org/Class_Reference/WP_Query#Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Parameters)
 *  Thread Starter [tarleton](https://wordpress.org/support/users/tarleton/)
 * (@tarleton)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/calling-current-category/#post-2137349)
 * Fantastic! Thanks mate. I’ve done a bit of searching on how to remove these duplicate
   tags. But a bit stuck I’ve found this: [http://wordpress.org/support/topic/having-duplicate-tags-ignored-in-only-one-query_post?replies=2](http://wordpress.org/support/topic/having-duplicate-tags-ignored-in-only-one-query_post?replies=2)
 * Which appears to be almost exactly same situtation I’m in however do not at all
   understand the solution being new to wordpress. Could someone please explain?
 * \Thanks again!
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/calling-current-category/#post-2137389)
 * the whole code got a bit longer, therefore a pastebin:
 * [http://pastebin.com/qfZQVGcS](http://pastebin.com/qfZQVGcS)
 * to avoid duplicates, the output of the tags is turned into an array, each tag
   array per post is added into a big array, then filtered into unique elements,
   sorted, and turned back into an output string in li form.
 * [http://www.php.net/manual/en/function.sort.php](http://www.php.net/manual/en/function.sort.php)
   
   [http://php.net/manual/en/function.array-merge.php](http://php.net/manual/en/function.array-merge.php)
   [http://php.net/manual/en/function.array-unique.php](http://php.net/manual/en/function.array-unique.php)
   [http://php.net/manual/en/function.explode.php](http://php.net/manual/en/function.explode.php)
   [http://www.php.net/manual/en/function.implode.php](http://www.php.net/manual/en/function.implode.php)
 * the code outputs the tags of all posts if not used within a category archive;
   to avoid this, you need to wrap the whole code into:
 *     ```
       <?php if( is_category() ) : ?>
       THE CODE HERE
       <?php endif; ?>
       ```
   
 *  Thread Starter [tarleton](https://wordpress.org/support/users/tarleton/)
 * (@tarleton)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/calling-current-category/#post-2137402)
 * Wow. I truly appreciate your help. I’m after one last modification. How can I
   get it to link to: /?category_name=CURRENTCATID&tag=TAGNAME
 * as as the moment is it just linking to /?tag=TAGNAME which obviously shows all
   posts with TAGNAME?
 * Once again…wow!
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/calling-current-category/#post-2137411)
 * [http://pastebin.com/zi2EN4NH](http://pastebin.com/zi2EN4NH)
 * i swapped `get_the_tag_list()` with `get_the_tags()` and rebuilt the links; [http://codex.wordpress.org/Function_Reference/get_the_tags](http://codex.wordpress.org/Function_Reference/get_the_tags)
 * hope this works 😉

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Calling Current Category?’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 2 participants
 * Last reply from: [Michael](https://wordpress.org/support/users/alchymyth/)
 * Last activity: [14 years, 12 months ago](https://wordpress.org/support/topic/calling-current-category/#post-2137411)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
