WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Grouping posts by category in tag archives (4 posts)

  1. vavroom
    Member
    Posted 4 years ago #

    I need to list posts on tag archives, grouped by the category they are in, with the category name above the group of posts. So the output would look something like this:

    <h1>'Brown' Tag</h1>
    	<h2>'Dogs' Category</h2>
    		<h3>Brown Labrador</h3>
    			<p>bla bla bla</p>
    		<h3>German Shepherd</h3>
    			<p>yada yada yada</p>
    	<h2>'Cats' Category</h2>
    		<h3>Siamese</h3>
    			<p>meow meow</p>
    		<h3>Havana Brown</h3>
    			<p>miou miou</p>

    I would have thought that was a fairly simple thing to do, but for the life of me, I can't seem to figure it out! I've tried using has_tag, in_category, query_post, and multiple variations thereof, to no avail. I've scoured the Codex, the forum, and am unable to find an answer, although there are many posts that *appear* similar, they are missing the bit of having the Category title above the post grouping.

    The closest I've come is using this code on archive.php:

    <?php
    if (is_tag()) {
    	while (have_posts()) : the_post();
    	if (in_category('117')) {
    		echo "<h2>Dogs</h2>";
    	} elseif (in_category('116')) {
    		echo "<h2>Cats</h2>";
    	} ?>
    		<h3><?php the_title(); ?></h3>
    		<?php the_excerpt(); ?>
    <?php endwhile; ?>
    <?php } else {
    //Something else
    } ?>

    However, the result adds the Category name before every post in each respective group.

    <h1>'Brown' Tag</h1>
    	<h2>'Dogs' Category</h2>
    		<h3>Brown Labrador</h3>
    			<p>bla bla bla</p>
    	<h2>'Dogs' Category</h2>
    		<h3>German Shepherd</h3>
    			<p>yada yada yada</p>
    	<h2>'Cats' Category</h2>
    		<h3>Siamese</h3>
    			<p>meow meow</p>
    	<h2>'Cats' Category</h2>
    		<h3>Havana Brown</h3>
    			<p>miou miou</p>

    Do you have any idea how I can fix this? Thank you

  2. vavroom
    Member
    Posted 4 years ago #

    No idea? Anyone?

  3. rickybuchanan
    Member
    Posted 4 years ago #

    Umm ... I don't actually know the answer but could you put orderby='category' into your get_posts and then only print the current category if it's different to the old one? The code would be something like this - but I haven't tested this:

    <?php
    if (is_tag()) {
    	query_posts($query_string.'&orderby=category');
    	$catname='';
    	$oldcat='';
    	while (have_posts()) : the_post();
    		$catname = $(get_the_category())[0];
    		if ($catname != $oldcat) {
    			/* new category */
    			echo '<h2>Category: ' . $catname . '</h2>';
    			$oldcat = $catname;
    		}
    		<h3><?php the_title(); ?></h3>
    		<?php the_excerpt(); ?>
    <?php endwhile; ?>
    <?php } else {
    //Something else
    } ?>

    The codex is rather vague on whether orderby=category works in this case and I don't have the energy to test it in a current WP. If that isn't an option, you could try nested queries for each category specifically ... it's ugly and computationally expensive but it'd work I think:

    <?php
    global $posts
    if (is_tag()) {
    	foreach((array('117', '116')) as $current_cat) {
    // Category name printing here - as above ...
    
    // Put your get_posts nested query here for categories in the current tag and in $current_cat then the usual:
    
    	while (have_posts()) : the_post();
    		<h3><?php the_title(); ?></h3>
    		<?php the_excerpt(); ?>
    <?php endwhile;
    
    } // end your foreach loop
    
    <?php } else {
    //Something else
    } ?>

    etc.

    As I said, that's out of my head and I haven't tested either - especially the second. Just ideas for you to go on. There's a codex page on nested loops that might help you - look it up.

    Good luck,
    Ricky

  4. vavroom
    Member
    Posted 4 years ago #

    Thank you Ricky! Haven't used your solution, but I found my answer in the solution!

    I had used query_posts to run a couple loops, but posts weren't restricted to the actual tag. The addition of "$query_string." resolved that!

    Thank you.

    For future reference, here's what I've done in the end:

    query_posts($query_string.'&cat=17');
    				echo '<h2>Cats</h2>';
    	while (have_posts()) : the_post();
    		?><h3><?php the_title(); ?></h3>
    		<?php the_excerpt();
    		endwhile;
    		wp_reset_query(); 
    
    	query_posts($query_string.'&cat=16');
    				echo '<h2>Dogs</h2>';
    	while (have_posts()) : the_post();
    		?><h3><?php the_title(); ?></h3>
    		<?php the_excerpt();
    		endwhile;

Topic Closed

This topic has been closed to new replies.

About this Topic