Support » Themes and Templates » Hooks and multiple loops

  • Hi

    First let me say that this and other sites have been an invaluable resource as I’ve slowly learned WordPress as a non-coder. However there are times when the more I read, the more confused I get. At these times I find that discussing with the community really helps me consolidate and understand what I’ve learned.

    I’m trying to add a second loop which will display posts from the category “featured”.

    I’ve done a lot of reading, and I think I’m almost there. But would appreciate any tips to streamline my code.

    I’m using this in functions.php of a thematic childtheme:

    function childtheme_catloop() {
    	$feat_query = new WP_Query(
    			'orderby' => 'rand',
    			'category_name' => 'featured',
    			'showposts' => 3
    	while ($feat_query->have_posts()) : $feat_query->the_post(); ?>
      		<!-- featured_loop -->
    	<?php endwhile;
    		echo '<div id="recent-header"class="section-header"><p>Recent Posts</p></div>';?>
      		<?php while (have_posts()) : the_post(); ?>
    			<!-- normal_loop -->
      	<?php endwhile;

    1 – Is there a more appropriate add_filter hook? This seems to be adding to rather than replacing the existing loop. Or is it better practice to add another function to remove the old loop. Something like:

    function remove_categoryloop() {
    add_action('init', 'remove_categoryloop')


    2 – Should I code the html for each loop? Or is there a hook I can call here? I don’t need to change any structure. I expected the_post() to work, but it doesn’t.

    Sorry if these are simple questions. I have been reading and struggling with this for a couple of days, but just can’t seem to make sense of it. I would be grateful for some pointers that would help me understand how and why it works.


Viewing 4 replies - 1 through 4 (of 4 total)
  • OK, after scratching my head some more, I’ve come up with the following solutions. If anyone can offer advice or ideas to improve this, I would be very grateful.

    1. I used the remove_action to remove the default loop, and then called my new childtheme_catloop function. But I had made a mistake in my remove_action syntax, and this was causing my headaches. The correct code is below, note the underscore in thematic_category_loop (I don’t yet understand why this works, so if anyone can point me to an explanation?…).

    function remove_categoryloop() {
    add_action('init', 'remove_categoryloop')

    2. My problem here was confusion between WordPress hooks and Thematic hooks. So to recreate the posts in the loop, I replaced <!– the loop –> above with these hooks:


    My full code (replacing <!– the loop –> above) is:

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

    Anyway, I hope this can be useful to someone else, and save them the many hours of head scratching that it’s taken me to get to grips with this simple problem!


    On a related note, I’d still love to know if there is a more efficient way to build the posts in my loop.

    The div=”id” code above is copied directly from the archive.php file in thematic.

    As this code already exists in the theme, is there a simple way to call it without needing to duplicate it?

    Hmmm, still trying to get to understand all this!!

    Hi Andrew –

    This Guide could help in a general sense:

    You are right that there are both Thematic action hooks that relate to different locations in your child themes (see guide), and also WordPress hooks which are more extensive — see:

    You should also note that the range of what is possible is broad, limited only by the functions you write.

    Lastly, as to your specific solution for adding a second loop — thanks for posting it. We sometimes use a query to pull content from a particular category — as in this example to get posts from category 13 and place them on the home page:

    if (is_home()) {
    query_posts( $query_string . '&cat=13&monthnum=' . date( 'n', current_time( 'timestamp' ) ) );


    Cheers –

    Hi Scott, thanks for pointing me towards some useful guides. I particularly like your example query. I had been trying to figure out how to add a date filter to my featured loops.

    Many thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hooks and multiple loops’ is closed to new replies.