Insert a Paged Blog Loop anywhere in your WordPress theme

<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('post_type=post&posts_per_page=5'.'&paged='.$paged); ?>

	<?php bootstrap_pagination();?>
	
	<?php while ($wp_query->have_posts()) : $wp_query->the_post();?>

		<!-- SAMPLE POST ENTRY MARKUP -->						
		<article id="post-<?php the_ID();?>">
			<header class="article-header">
				<a href="<?php the_permalink();?>"><h2 class="entry-title"><?php the_title();?></h2></a>
			</header>					
			<div class="article-body"><?php the_excerpt();?></div>
		</article>
		<!-- END SAMPLE POST ENTRY MARKUP -->
		
	<?php endwhile;?>
					
	<?php bootstrap_pagination();?>

<?php $wp_query = null; $wp_query = $temp;?>

Note that the <?php bootstrap_pagination();?> function has already been discussed in our post “Add Twitter Bootstrap Pagination to your WordPress Theme“.

I won’t spend too much time explaining this one. Just know that this will start an independent loop of your choice anywhere you want in your theme templates. And it correctly incorporates wp’s pagination feature, which can be tricky. I primarily use this code in a template I call blog.php. Many clients want either a static home page, or magazine-style home page, but still want a blog-reel displayed elsewhere (a la the common index.php). My blog.php template featuring the above code can be easily added to any internal page of their choice. Of course, there are countless ways to customize the query parameters.

   
Leave a Comment
User

Mail

Globe

Comment