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( array( '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; } add_filter('thematic_categoryloop','childtheme_catloop');
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() { remove_action('thematic_categoryloop','thematic_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.
THANK YOU!!
- The topic ‘Hooks and multiple loops’ is closed to new replies.