Adding a Loop to a WordPress Page Template with WP_Query and Pagination

Its pretty easy to add a loop to a page template but having pagination will provide some roadblocks if you’re trying to use the standard previous and next template tags. The solution I came up with this afternoon is pretty basic and if there’s a better alternative I’m open to hear them.

Place this after get_header() within your page template.

$paged = (get_query_var('paged')) ? (int) get_query_var('paged') : 1;
$page_link = get_permalink($id);

Not sure what type of loop you want to create but here’s what I recently used under the page content.

<?php
	$events_query = new WP_Query('category_name=Custom&amp;paged='.$paged);
	while ($events_query->have_posts()) : $events_query->the_post();
		// post markup would go here, e.g. the_excerpt();
	endwhile;
?>

I also wrapped my page content so it only showed on the first page and not the paginated pages.

<?php
	if($paged == 1){
		// all the page content markup, e.g. the_content();
	}
?>

After those updates you’ll have a page template that returns a loop of posts under the “Custom” category.

The last step is adding the pagination links. Here’s the markup I used.

<div class="pagination">
			<span class="previous">
				<a href="<?php echo $link ?>page/<?php echo $paged + 1; ?>">&amp;laquo; Previous</a>
			</span>
			<?php if($paged != 1): ?>
			<span class="next">
				<a href="<?php echo $link ?>page/<?php echo $paged - 1; ?>">Next &amp;raquo;</a>
			</span>
			<?php endif; ?>
</div>

So, is there a better way out there? I hope so, since the previous and next links show regardless if there’s posts or not, and I don’t feel like writing those queries for this tutorial :).

About the Author, Dan Cameron:

I'm the owner and solution engineer , a web solutions company that specializes in web development including WordPress.

I started my first blog in 2003 and transitioned to WordPress in 2004. Since moving to WordPress I've written a few plugins and themes for public consumption. Lately I'm busy engineering/building/coding and have only been able to share a few code snippets.

If you're in need of some web development, web design or custom WordPress plugins and/or themes contact me, I'll be happy to discuss it with you.

Read More »

Comment Feed

4 Responses

  1. I think I have a quick solution to at least hide the previous link if there aren't any more posts.

    <?php if (count($subposts->posts) == get_option('posts_per_page')): ?>

    <a href=”<?php echo $page_link ?>page/<?php echo $paged + 1; ?>”>Previous

    <?php endif ?>

    If you compare the posts returned to the posts_per_page you will know if you are at the end!

    It's not perfect, but it's quicky + dirty :)

  2. It's probably better to grab the found_posts from the query. Here's something that I'm starting use at http://sproutventure.com/portfolio

    $posts_per_page = intval(get_query_var('posts_per_page'));$pages = intval(ceil($wp_query->found_posts / $posts_per_page));
    
    if ( $paged < $pages ) :
  3. It's probably better to grab the found_posts from the query. Here's something that I'm starting use at http://sproutventure.com/portfolio

    $posts_per_page = intval(get_query_var('posts_per_page'));$pages = intval(ceil($wp_query->found_posts / $posts_per_page));
    
    if ( $paged < $pages ) :
  4. Thank You. Your idea is great. I am very grateful to you.

    lincolnbdMay 17, 2010 @ 12:19 pm



Some HTML is OK

or, reply to this post via trackback.

blog comments powered by Disqus