WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Tag archive from a single category - Is this possible? (34 posts)

  1. huckmag
    Member
    Posted 6 years ago #

    Hi all,

    Does anyone know if it's possible to show tag archives from a single category?

    I run a magazine website using WordPress as a CMS. Most posts are tagged for use in meta keywords.

    However I would like to show the tags in the 'Blog' category only, and if people view /tag/snow/ for example, they only see posts tagged with 'snow' in the 'Blog' category...even if otehr posts throughout the site are tagged with this term.

    Any help with this would be massively appreciated.

    You can view my site here:
    http://www.huckmagazine.com

    Thank you!
    Alex

  2. mores
    Member
    Posted 6 years ago #

    Hmm, the problem is I can't really see how you built your site. I really like it.

    The links at the top, do they call categories?
    category "blog", category "features" etc?

    Then you can use category-#.php (where # is the category number) to make the blog category show tags and the other categories do not show them.
    Now, how to edit the tag-link to remain in that one "blog" category is something I haven't actually tried yet. But according to this post, you can create a tag.php file and handle the display of tags. So, if you take this tag.php and make it look only in the "blog" category, you'd be all set.

    PS: your site is invisible when using safari for mac.

  3. huckmag
    Member
    Posted 6 years ago #

    Hi Mores

    Yes most of the links at the top are categories (well the ones that have /content/categoryname/ structure. The rest are pages.

    I tried making a tag.php and then putting query posts for the blog category in the loop but when i went to /tag/snow/ for example, it just showed up all posts in the blog category not the ones tagged with certain words.

    Example here:
    http://www.huckmagazine.com/tag/snow/

    The code for my tag.php is:

    <?php get_header(); ?>
    <?php query_posts('category_name=blog');
    ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <h2 class="heading">Blog posts tagged with '<?php single_tag_title(); ?>'<span></span></h2>
    <div class="post">
    	<h3><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    	<div class="credits">Written by <?php the_author(); ?>  |  <?php the_time('l, F jS, Y') ?></div>
    <?php echo c2c_get_custom('Image URL', '<span class="clear-space2"></span><img src="', '" width="421" alt="blog post image" /><span class="clear"></span>
    '); ?>
    <div class="postentryblog">
    					<?php the_content() ?>
    </div>
    	</div>
    <?php endwhile; else : ?>
    
    	<div class="post">
    
    		<h4><?php _e('Not Found'); ?></h4>
    
    <div class="postentry">
    
    		<p><?php _e('Sorry, but the page you requested cannot be found.'); ?></p>
    
    		</div>
    
    		</div>
    	<?php endif; ?>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Any ideas?

    PS. I"ve been messing with the site so maybe you looked at the site in Safari when it was briefly down. Can you try again? works fine for me (both Safari 3 and Safari 2)

    Alex

  4. mores
    Member
    Posted 6 years ago #

    try using the category number instead of writing it.
    that's what they do here: http://codex.wordpress.org/Template_Tags/query_posts

    PS: yes, huckmagazine.com works in Safari again :)

  5. huckmag
    Member
    Posted 6 years ago #

    I still get the same thing if I use:

    <?php get_header(); ?>
    
    <?php
    query_posts('cat=24'); ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    <div class="post">
    	<h3><a>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    	<div class="credits">Written by <?php the_author(); ?>  |  <?php the_time('l, F jS, Y') ?></div>
    <?php echo c2c_get_custom('Image URL', '<span class="clear-space2"></span><img src="', '" width="421" alt="blog post image" /><span class="clear"></span>
    '); ?>
    <div class="postentryblog">
    					<?php the_content() ?>
    </div>
    	</div>
    		<?php endwhile; ?>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    It just lists all posts from category 24 (the blog category) and not the posts tagged with specific words.

  6. mores
    Member
    Posted 6 years ago #

    Ah, i think i misunderstood. I ignored the fact that you don't get the tag you want.

    According to this page, you can use tags as well in the query_string thing.
    Like query_posts('tag=cooking');
    But since you don't know what tag you're using when you're on http://www.huckmagazine.com/tag/snow/ for example, try using the single_tag_title function.

    Something like <?php query_posts('category_name=blog&tag='.single_tag_title) ?>

    Not sure how to add the single_tag_title to the parameters of query_posts.

  7. huckmag
    Member
    Posted 6 years ago #

    I think we're getting closer to a solution! Thank you for your time Mores.

    Just need to work out how to add the single tag title to the query_posts...

    Does anyone out there in WordPress land have the answer?

  8. mores
    Member
    Posted 6 years ago #

    Try something like this:

    <?php
    $current_tag = single_tag_title("", false);
    query_posts('category_name=blog&tag=$current_tag')
    ?>

    Inspired by one of these examples

  9. huckmag
    Member
    Posted 6 years ago #

    argh the page is coming up blank - looks like single_tag_title won't work inside query_posts.


    http://www.huckmagazine.com/tag/fashion/

    Any ideas how we could get round that...I'm sure we're so close!

  10. huckmag
    Member
    Posted 6 years ago #

    shameless bump - we nearly got this resolved, anyone got any suggestions?

    - Alex

  11. SimonJ
    Member
    Posted 6 years ago #

    Hi huckmag.

    I don't know if I understand well.

    But, from what I can tell, you want to display posts from one category then, from this category, only posts with a certain tag.

    In english : show post from category ABC tagged with XYZ.

    I discussed this issue here :
    http://wordpress.org/support/topic/146199?replies=6

    With no solution...

    In wordpredss language, it should be :

    query_posts('cat=ABC&tag=XYZ');

    But it don't works... From what I can tell, and I don't understand why, you can't use "cat" AND "tag" in the same query... I remove "cat" or "tag", I have a result, but with both, it returns a blank result. Nothing appears.

    The same problem is exposed here, still with no solution :
    http://wordpress.org/support/topic/152261?replies=5

    So, if a wordpress master could explain if it's possible to use "tag" AND "cat" in the same query, it would be very helpful..

    S.

  12. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    <?php
    $current_tag = single_tag_title("", false);
    query_posts('category_name=blog&tag=$current_tag')
    ?>

    That won't work. Change those single quotes to double quotes. Like this:

    <?php
    $current_tag = single_tag_title("", false);
    query_posts("category_name=blog&tag=$current_tag");
    ?>

    Also, I see no reason that you cannot combine a category and tag query. Should work fine. However, every thread I'm seeing, this one, the one linked to above, they all seem to misunderstand the parameters for query_posts.

    cat= the NUMBER of the category (multiples separated by commas).
    tag= the NAME of the tag.

    So query_posts('cat=3&tag=bob'); should work fine.

  13. SimonJ
    Member
    Posted 6 years ago #

    Otto42 : "I see no reason that you cannot combine a category and tag query. Should work fine."

    Thanks for your reply Otto42. Like you, I don't see any reason, but it don't works and from what I can read on the forum, I'm not alone.. ;-)

    Just try it if you have some time to do so... Make a simple loop with a query_posts with tag & cat, and tell me your result. I'm really in a dead end on that issue.

    Thanks!

    S.

    EDIT : Otto42 : "cat= the NUMBER of the category (multiples separated by commas).
    tag= the NAME of the tag."

    :-)... Belive me, I have dozens of loops with query_posts and I always use the cat ID number... All I do is to add &tag='wathever'... And It don't works...

    My first guess was that the problem is that category and tags are both "terms"...

    S.

  14. huckmag
    Member
    Posted 6 years ago #

    Otto42 - I've tried doing what you stated and put

    <?php
    $current_tag = single_tag_title("", false);
    query_posts("category_name=blog&tag=$current_tag");
    ?>

    This simply comes up with a blank page.
    As you can see here:

    http://www.huckmagazine.com/tag/surf/

  15. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Ahhh! Okay, yes, I see the problem..

    It's a problem in the query processing logic. Specifying a tag and a category at the same time will always result in no results.

    The reason is that the query gets these added to it:

    ...AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.taxonomy = 'post_tag'...

    Both of those cannot be true.

    This is a bug, I'll put it into trac.

  16. huckmag
    Member
    Posted 6 years ago #

    Thanks for your help Otto42 - would love to have this fixed at some point.

    - Alex

  17. SimonJ
    Member
    Posted 6 years ago #

    Otto42 : "This is a bug, I'll put it into trac."

    Ahhhhh! I'm sooooo happy to discover that the problem is not between my ears!

    :-)

    Thanks Otto42!

    S.

  18. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Here's a workaround. This should give the desired results, although there's still a bug with the way the tag and cat is handled.

    query_posts(array(
    'cat'=>'3',
    'tag_slug__and'=>array('bob'),
    ) );

    Trac link for this issue: http://trac.wordpress.org/ticket/5433

  19. SimonJ
    Member
    Posted 6 years ago #

    Thanks! I'll try it right now!

    S.

  20. SimonJ
    Member
    Posted 6 years ago #

    I know, I know, I said "right now"... but... :-) YEAH...

    I just tried your code Otto42, and it works great, exactly what I wanted! Thank you so much.

    By the way, do you know how I could include in that code an "orderby" and a "showpost" ?

    Thanks again!

    S.

  21. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Sure.

    query_posts(array(
    'cat'=>'3',
    'tag_slug__and'=>array('bob'),
    'orderby'=>'whatever',
    'showpost'=>'also whatever',
    ) );

    When using the parameters as an array like this, you just include them separately. Anywhere that WordPress uses the "query string" style of parameters, this style can also be used.

  22. SimonJ
    Member
    Posted 6 years ago #

    Nice! Thanks again.

    S.

  23. huckmag
    Member
    Posted 6 years ago #

    Otto42 - that works great for me too, but is there a way to combine it with this...

    <?php
    $current_tag = single_tag_title("", false);
    query_posts("category_name=blog&tag=$current_tag");
    ?>

    So I can get it working with the currently viewed tag? If I just do this...

    <?php
    $current_tag = single_tag_title("", false);
    query_posts(array(
    'cat'=>'24',
    'tag_slug__and'=>array('$current_tag'),
    ) );
    ?>

    I get a blank page again.
    Any ideas on a way to get it working?

    Thanks for helping me out on this
    -Alex

  24. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    huckmag: Don't put $current_tag in the single quotes.

    $current_tag = single_tag_title("", false);
    query_posts(array(
    'cat'=>'24',
    'tag_slug__and'=>array($current_tag),
    ) );

    Single quotes make a string literal.
    Double quotes make an interpreted string (variables are expanded).

    But when you already have a string in a variable, you don't need quotes around it.

  25. huckmag
    Member
    Posted 6 years ago #

    Otto42, you are a hero, thank you so much for making this possible!

  26. huckmag
    Member
    Posted 6 years ago #

    PS. if you want to see this fix in action check...
    http://www.huckmagazine.com/tag/skate/

    Where it only pulls posts from the Blog category and leaves other categories such as Feature well alone even though many of those have 'skate' as a tag.

    Thanks again for the great WP community :)

  27. Indojepang
    Member
    Posted 6 years ago #

    This is very interesting!
    Hi Otto 42, how to make these category tags show as a tag cloud?
    thanks..

  28. OmeRoon
    Member
    Posted 5 years ago #

    Thanks for this great info, I was struggling with the same problem but Otto42's fix works like a dream.

    Cheers mate! 8-)

  29. OmeRoon
    Member
    Posted 5 years ago #

    Unfortunately, it only works with tag_slug__and and not with tag_slug__in (which is a bit more obvious when using tags). Using tag_slug__in the result is a blank page. 8-(

    Anyone got any suggestions how to fetch this data?

    Example: category 1 AND (tag 1 OR tag 2)

    $aQuery = "";
    
    $aTags = explode(",",$tag);
    
    if ($cat != "" &&  $tag != "") {
    	if (is_numeric($aTags[0])) {
    		$aQuery = array( 'cat'=>$cat, 'tag__in'=>$aTags);
    	}
    	else {
    		$aQuery = array( 'cat'=>$cat, 'tag_slug__in'=>$aTags);
    	}
    }
    else if ($cat != "") {
    	$aQuery = 'cat='.$cat;
    }
    else if ($tag != "") {
    	if (is_numeric($aTags[0])) {
    		$aQuery = array('tag__in'=>$aTags);
    	}
    	else {
    		$aQuery = array('tag_slug__in'=>$aTags);
    	}
    }
    query_posts($aQuery);

    This works perfectly with tag_slug__and and tag__and... any suggestions how to get it working with tag_slug__in and tag__in?

  30. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    This works perfectly with tag_slug__and and tag__and... any suggestions how to get it working with tag_slug__in and tag__in?

    Simple. You don't.

    With the current code base, tag__in/tag_slug__in and category__in (same as "cat") are utterly incompatible. Period. There is no workaround, there is no fix. Without a large rewrite of the query code, it simply can not be done.

    This is a known problem. It's in the bug tracker, but not really considered high priority at the moment.

Topic Closed

This topic has been closed to new replies.

About this Topic