Pagination for multi-page posts in WordPress

August 13th, 2011 (6 months ago)

Pagination CollageFor my new blog I had it in mind to do small tutorials on newly acquired skills as I learned them. As it turned out, the learning was rather instantaneous as I ran into the problem of paginating multi-page posts in WordPress. While WordPress does offer the wp_link_pages() function that automatically puts “Next” and “Previous” page links into a post, I was rather unhappy with how it looked and wanted something more customized. So I scoured the web for answers and was dismayed to see a lack of documentation about the subject. But being quite determined, I eventually settled upon a solution. And seeing as I came across this problem on my way to creating my first tutorial, it’s only fitting that this should be the subject of my first tutorial.

This first page will outline the basic concept of paginating a multi-page post in WordPress. The second page will show you how to create “Previous” and “Next” links for your own customization purposes, as well as show you how to create the actual permalinks for the anchor tags. So, without further ado, let’s discuss how to paginate a multi-page post in WordPress.

 

Assessing the situation

Before plunging into anything I usually make a checklist of things that I would need to do in order to pull off whatever it is I’m trying to accomplish. After a few logical deductions I came up with the following list.

  1. Check if the post is split up into pages.
  2. Define the total amount of pages.
  3. If we’re currently in a multi-page post, define what page we’re on.
  4. Create links to the other pages.

 

1 and 2 with one stone!

In WordPress, the way we split posts up into pages is by inserting this code text where we want to split the page.

<--nextpage-->

So, in order to check if the current post has multiple pages, all we need to do is search through the post’s content for any instances of that code. Using the substr_count() function in PHP, we’ll be able to get the total number of occurrences of that code. If you’re inside the Loop, you could use the following code to accomplish this.

$max_pages = substr_count( $post->post_content , '<!--nextpage-->' ) + 1;

Note that we add 1 to the substr_count() result because every time we split the post we’re adding to the total number of pages we began with, which is 1. We store this value into a variable because we’ll be reusing this information later on.

 

What page are we on?

To determine what the current page is we turn to the wp_query class which carries useful information regarding the requests we make to a WordPress blog. Among its numerous properties and methods is the query_vars array which holds the query variables and their values. Within this array resides the “page” variable if we should happen to be inside of a multi-page post or page. If it’s not set, we can clearly deduce we’re on the first page. Otherwise it would let us know if we’re on page two, or three, and so on.

$page_num = $wp_query->query_vars[ 'page' ];
if( !$page_num ) { $page_num = 1; }

The first line of code stores the value of “page” into a variable. Immediately afterward, we check to see if it’s actually set. If it’s not, then we set our page number variable to 1.

 

Create the page links.

Now that we have the total number of pages and the current page number we can now make a list of page links. By using a “for” loop we’ll be able to iterate through all the page numbers from one to however many pages there are in the post. See the code below.

for ( $i = 1 ; $i < $max_pages + 1 ; $i++ ) {
echo '<a>' . $i . '</a>' ;
}

Voila! We have now provided a pagination scheme for a multi-page post in WordPress.

 

That’s it?!?!

Well, that’s the basic gist of how we create pagination. The above example will do fine for most multi-page posts. I don’t imagine how anyone would need any heavy duty post pagination since it would be quite insane to break a single post into anything more than 20 or so pages. However, I could be wrong and there may be some good reasons to do so. And if I am, then that’ll be a topic for another tutorial!

For now, if you’re interested in something a bit more in depth, move on to the next page where we delve into constructing “Previous” and “Next” links. And when you’re done with that we wrap things up with permalink creation and search engine optimization issues regarding multi-page posts.

 
Next Page

TAGS: , ,

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">