ThemeShaper Forums » Thematic

[closed]

Group results of category archive pages by sub-categroies

(7 posts)
  • Started 3 years ago by PitaMaria
  • Latest reply from PitaMaria
  • This topic is resolved
  1. PitaMaria
    Member

    My example page is here: http://coloradohealthstory.org/category/location/. This page serves up excerpts of all pages with class.category-location, just like it should. Now, I need some organization to how those excerpts appear: I want the page to group each child category (.category-aurora, .category-denver, etc.) in alpha order, with an H2 of the child category name, like this:

    Aurora
    Jimmy Sample
    [excerpt]
    Jenny Sample
    [excerpt]
    Jane Sample
    [excerpt]

    Denver
    José Sample
    [excerpt]
    Joe Sample
    [excerpt]
    John Sample
    [excerpt]

    Thornton
    Janet Sample
    [excerpt]

    ...and so on.

    I'd also like to automatically check for new child categories and have them considered in this equation. I found something here but am not sure if it will work: http://forums.themeshaper.com/topic/automatically-check-for-new-categories#post-12158

    I'm running a Thematic child theme and, if possible, would like to accomplish this in my functions.php rather than hard code it into templates. Ultimately I'll need an array of these types of organizations (by .category-topic, .category-keyword, etc.)

    Apologies for starting a new topic on this but my head is spinning looking at the different ways requests like this could be handled, and my head is spinning. Any help would be greatly appreciated. If any additional information is need please let me know.

    Posted 3 years ago #
  2. middlesister
    Member

    You will need to use a childtheme_override function for the category loop, and use a custom loop for the posts under each subcategory heading. I tested this code and it seems to work:

    function childtheme_override_category_loop() {
    	$parent_cat_id = '3';              // this would be your parent category
    	if( is_category($parent_cat_id) ) {
    		$subcat_args = array(
    		  'orderby'  => 'name',
    		  'order'    => 'ASC',
    		  'child_of' => $parent_cat_id
    		);
    		$subcategories = get_categories($subcat_args);
    		foreach($subcategories as $subcat) {
    			echo '<h2>'. $subcat->name . '</h2>';
    
    			$post_args = array(
    				'cat'      => $subcat->cat_ID,
    				'orderby'  => 'name',
    				'order'    => 'ASC'
    			);
    			$subcat_posts = new WP_Query( $post_args );
    
    			while ($subcat_posts->have_posts()) : $subcat_posts->the_post()
    
                                    // your loop for the posts goes here
    
    			endwhile;
    			wp_reset_postdata();
    		}
    	} else {
    
    		// your normal category loop goes here
    
    	}
    }
    Posted 3 years ago #
  3. PitaMaria
    Member

    @middlesister: First, thanks for taking the time to offer a tested solution to my issue; I really appreciate it. Everything looks super-thorough and I've (finally) added it to my functions.php.

    That said, I have yet resolved the areas where you say I must drop in my "loop for the posts" and my "normal category loop". I'm so green that just thinking about The Loop makes me break out into hives, let alone finding the right code to copy and paste into these two areas.

    Knowing that I'm using a child theme of Thematic, can you tell me exactly where to find those two bits of code? If it's in the index.php that came out of the box with Thematic, can you tell me which line numbers make up those bits? Tell me if you need me to email you some files and I'd be happy to.

    Thanks again for your attention to my issue and thanks in advance for your reply, especially since I took so long to take this up with you.

    Posted 3 years ago #
  4. middlesister
    Member

    You're welcome :)
    Well the loops in thematic can be found in the file content-extensions.php. The category loop looks like this

    while (have_posts()) : the_post(); 
    
    	thematic_abovepost(); ?>
    
    	<div id="post-<?php the_ID();
    	echo '" ';
    	if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    		post_class();
    		echo '>';
    	} else {
    		echo 'class="';
    		thematic_post_class();
    		echo '">';
    	}
    		thematic_postheader(); ?>
    		<div class="entry-content">
    		<?php thematic_content(); ?>
    
    		</div><!-- .entry-content -->
    		<?php thematic_postfooter(); ?>
    	</div><!-- #post -->
    
    	<?php 
    
    	thematic_belowpost();
    
    endwhile;

    You can paste that as the "normal" category loop, and if you don't want to change anything in the layout you can use the same loop for the other posts. Just copy the parts from thematic_abovepost() to thematic_belowpost() - without the while/endwhile lines - and paste as the loop for your posts.

    Posted 3 years ago #
  5. PitaMaria
    Member

    @middlesister: You're so great. I've implemented your solution and it works like a charm. I just need it to do one more thing: I need it to work for category IDs 3, 4 and 5. As you'll see in my setup below, it's working just for category 3...

    function childtheme_override_category_loop() {
    	$parent_cat_id = '3'; // this would be your parent category
    	if( is_category($parent_cat_id) ) {
    		$subcat_args = array(
    		  'orderby'  => 'name',
    		  'order'    => 'ASC',
    		  'child_of' => $parent_cat_id
    		);
    		$subcategories = get_categories($subcat_args);
    		foreach($subcategories as $subcat) {
    			echo '<h4>'. $subcat->name . '</h4>';
    			$post_args = array(
    				'cat'      => $subcat->cat_ID,
    				'orderby'  => 'name',
    				'order'    => 'ASC'
    			);
    			$subcat_posts = new WP_Query( $post_args );
    
    			while ($subcat_posts->have_posts()) : $subcat_posts->the_post()
    				// begin loop for the posts
    					?>
    					<div id="post-<?php the_ID();
    					echo '" ';
    					if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    						post_class();
    						echo '>';
    					} else {
    						echo 'class="';
    						thematic_post_class();
    						echo '">';
    					}
    						thematic_postheader(); ?>
    						<div class="entry-content">
    						<?php thematic_content(); ?>
    						</div><!-- .entry-content -->
    						<?php thematic_postfooter(); ?>
    					</div><!-- #post -->
    					<?php
    				// end loop for the posts
    			endwhile;
    			wp_reset_postdata();
    		}
    	} else {
    		// begin normal category loop
    			while (have_posts()) : the_post();
    				thematic_abovepost(); ?>
    				<div id="post-<?php the_ID();
    				echo '" ';
    				if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    					post_class();
    					echo '>';
    				} else {
    					echo 'class="';
    					thematic_post_class();
    					echo '">';
    				}
    					thematic_postheader(); ?>
    					<div class="entry-content">
    					<?php thematic_content(); ?>
    					</div><!-- .entry-content -->
    					<?php thematic_postfooter(); ?>
    				</div><!-- #post -->
    				<?php
    				thematic_belowpost();
    			endwhile;
    		// end normal category loop
    	}
    }

    So, could you shed some light on how I get it to work for categories 4 and 5? As always, many thanks in advance.

    Posted 3 years ago #
  6. middlesister
    Member

    Well that only needs a small change. In the beginning, add all the category IDs that you need to the is_category() conditional and then change the $subcat_args to use the current category. Like so:

    function childtheme_override_category_loop() {
    	if( is_category( array(3,4,5) ) ) {     // all your parent categories in an array
    		$this_cat_id = get_query_var('cat');   // fetches which category we are on
    		$subcat_args = array(
    		  'orderby'  => 'name',
    		  'order'    => 'ASC',
    		  'child_of' => $this_cat_id
    		);

    And the rest is the same.

    Posted 3 years ago #
  7. PitaMaria
    Member

    @middlesister: This works perfectly, of course. I can't thank you enough. You rock!

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.