WordPress.org

Ready to get started?Download WordPress

Forums

Calling Current Category? (6 posts)

  1. tarleton
    Member
    Posted 3 years ago #

    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
    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.

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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

  3. tarleton
    Member
    Posted 3 years ago #

    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

    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!

  4. alchymyth
    Forum Moderator
    Posted 3 years ago #

    the whole code got a bit longer, therefore a pastebin:

    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://php.net/manual/en/function.array-merge.php
    http://php.net/manual/en/function.array-unique.php
    http://php.net/manual/en/function.explode.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; ?>
  5. tarleton
    Member
    Posted 3 years ago #

    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!

  6. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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

    hope this works ;-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.