Support » Fixing WordPress » Tags Listed by Category, Need Tag Links by Category, too

  • I dug around and found some code to list (linked) tags by category, and it works great. Unfortunately, when I click on one of the tag links, it takes me to a list of posts by that tag but the list of posts isn’t category-specific. Is it possible to do this?

    I have a category-3.php file with the following:

    <?php $args = array(
    ‘categories’ => ‘3’
    $tags = get_category_tags($args);

    $content .= ”

    echo $content;

    When I go to this page, and click on California (Category 3) I get the list of tags (unstyled as yet, so no judging!) from the posts categorized ‘California’ exactly as I want, but when I click on, for instance, ‘alameda’ I end up with posts from other categories, in addition to those from California.

    It seems like this should be a standard, straight-forward function, but I can’t find it anywhere. Let me know if I need to clarify anything.


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


    The problem here is that you are using get_category_tags with an argument to limit the tags to those assigned to the currently viewed category. However, when you get to the tags page (tag.php or archive.php) you are not using a query to get the category associated with the tag clicked from the previous click! (confusing, I know)

    What you need to do, is on your category-3.php page, where you are shooting out the tag links, you’ll want to pass the category ID along with it somehow… either in the URL or by POST. In either case, on your tag.php or archive.php (whichever you are using) modify the loop following these instructions:

    Something like:

    global $query_string;
    query_posts( $query_string . '&category=[category_number_from_URL_or_POST]' );

    Of course, replace [category_number_from_URL_or_POST] with whatever method you pass the category along. You can also use an array if you have more than one category you wish to include.

    Yes, that’s the tricky part… I’m not very experienced with PHP, as you can likely tell, but it seems that this should be a common request.

    Isn’t there a way to return links limited by category in sort of the same way the list is created by category?

    I’ll give your suggestion a try though, thanks very much for the response!



    Not that I know of… and the function you are using is a custom function anyway, not a WordPress default function. Here’s what I would do… Since you already know the category ID (it’s the page you’re on anyway category-3.php) I would tack it onto the URL:

    foreach ($tags as $tag) {
    $content .= "<a href=\"$tag->tag_link\?cat=3">$tag->tag_name</a>";

    Then on the tags.php page, or the archive.php page, whichever you are using, use my method above and add:

    global $query_string;
    query_posts( $query_string . '&cat=' . $_GET['cat'] );

    to the top of the page. See if that works.

    Actually, I think tagging the category on to the end of the initial link is all I need, thanks! I had to make one minor adjustment by moving ?cat-3 over to immediately after tag_link, but other than that it seems to be perfect (see below):

    foreach ($tags as $tag) {
    $content .= “tag_link?cat=3\”>$tag->tag_name“;

    I haven’t made any change to the tag template and, so far, it’s perfect! I’ll let you know if I run into any problems, but I think this is just what I need. I can customize this string for each category template and leave it off of the regular category template and I’m in business.

    Thanks again!



    You are welcome

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Tags Listed by Category, Need Tag Links by Category, too’ is closed to new replies.