Pagination in posts

And this is Page 2. Neat, huh? ;)

However, it’s not that straightforward, I’m afraid. There’s a bit more tweaking to be done to your blog’s single.php template. But before we go on, check to see if the template you’re using has the following template tag:

wp_link_pages()

While the <!--nextpage--> tag tells WordPress that a post must be split into sub-pages, the above function is what tells WordPress how to display the page numbers (or “next / previous page” text, if the case may be) and how to link to their corresponding sub-pages.

If the function is not present in single.php, here’s how you can use it. Remember, it must be placed within The Loop. I suggest you put it after the_content(). Below is how I did it for The WordPress Evangelist:

<div class="entry">
	<?php the_content(); ?>
	<?php wp_link_pages('before=<div class="postnav">| &after= |</div>&pagelink=<span class="pagelink">Page %</span>'); ?>

	<?php if (function_exists('sociable_html')) echo sociable_html(); ?>
	<?php if (function_exists('related_posts')) related_posts(); ?>
</div><!-- /.entry -->

N.B.: The two plugin functions that’re being called after wp_link_pages() — Sociable and YARPP — are there because I didn’t want those two plugins to automatically insert their stuff, which they would do at the bottom of the post’s body. Since I’m calling the wp_link_pages() function after the_content(), the pagination links would appear below Sociable’s icons and YARPP’s related posts links. But inserting the plugin functions manually solves this design issue.

The wp_link_pages() function works even without its parameters, but it would be a good idea to play around with some of them so that you could get page links to display in a way that appeals to you and your readers. What do you think of mine? :)

| Page 1 Page 2 |


4 Responses

Write a comment
  1. methinks it’s neat. thx for sharing!

    dennis dm 18 July 2010 at 4:51 PM Permalink
  2. I did this, but the link to Page 2 of the post did not show up for my RSS reader and email subscribers. How do you make the link to Page 2 show up in the RSS feed?

    Nate@House of Annie 21 October 2010 at 11:50 AM Permalink
    • No, the pagination links wouldn't appear in RSS feeds, because they live outside of the content. I haven't encountered any workarounds to address this, though. If I discover a way to do it, I'll post it here!

      Blogie 22 October 2010 at 4:52 PM Permalink

Write a Comment

Commenter Gravatar