Running a second WordPress loop alongside the main loop

WordPress comes standard as a site with one blog loop, header, sidebar, and footer.  But what if you want to run 2 blog loops on the same page?  Well, there’s an app for that… errr… function for that.  It may seem as easy as copy and pasting the while loop that is in the main index.php file, but the way WordPress is structured, duplicating the same loop in that function will cause an error.  The reason that this might be set up is to show all of your blog posts, and also show posts only within a specific category.  This is how you would set this up:

Create the custom loop using  WP_Query()

<?php

$custom_loop = new WP_Query(‘showposts=3&category_name=Side Projects’);

// Within the Query you can pass the variables you want within this loop.  Here I have it showing 3 posts only within the category named ‘Side Projects

//Then run the custom WordPress loop

if ( $custom_loop->have_posts() ) :
while ( $custom_loop->have_posts() ) : $custom_loop->the_post(); ?>
/// this is where you will put your post information that you want to display including the headline and body copy
/// Ending the loop
endwhile;
endif;
?>
Thats it!
Comments are closed.