Paginated Posts

Out of the box, WordPress supports the ability to paginate individual posts.

For those who aren’t familiar, it means that you’re able to literally paginate your posts such that a post may consist of multiple pages (kind of a weird concept, huh?).

Paginated Posts

Some people may use this feature, some people may not; however, if you’re working on a theme, then you need to be prepared for this case and provide appropriate styling.

But the way if which you determine if a post is paginated or not can lead to a bit of cluttered code, so in order to keep the code as readable as possible (and to keep the logic separated from the template), here’s one way that you can check is a post is paginated.

The Post is Paginated?

WordPress has a global variable, $multipage that will denote whether or not a post is paginated or not.

In the case that a given post is not paginated, then $multipage will be equal to 0. This means that’s it’s really easy to write a function to encapsulate this logic and return TRUE if the post is paginated:

To display the links to each of the individual posts pages, you use the function wp_link_pages which is customizable (as seen in the linked Codex article).

Normally, I wrap this in some sort of container, and then I wrap the container in a conditional that allows me to conditionally display the container based on of the post is paginated.

In using the function above, it looks like this:

It’s a bit cleaner, right?

A Separation of Concerns

In its most basic form, this is a separation of concerns – logic specifically dealing with determining if a given post is paginated is located in its own function, and then the template file is able to call the function to determine whether or not to display a container for the list of pages.

Anyway, this is a relatively simple idea, but I’ve found it to come in handy in a couple of projects and thought it was worth sharing.

Next, you can then use this function in your templates to create a more readable conditional on whether or not to display the container for the post pagination links that will allow you to provide styling that only appears if those links appear.


Join the conversation! 7 Comments

  1. Thanks for sharing this Tom! I didn’t know that variable existed. I found another way to figure this out, but it was no where as clean as this.

  2. Thanks for sharing Tom! This is definitely a cure for the extremely long post!

    • I’m personally not a fan of paginating posts themselves – I’d rather see shorter posts as part of a series, but that’s just me, you know?

      If you’re building a theme, you need to be able to account for this :).

  3. I agree Tom, I believe that self paginated posts are a very bad and lazy thing.

  4. I’m curious why there’s no function for this. The WordPress handbook advises against using global variables but here we are.

    • We are advised against using global variables, and we should, but there are some things in Core that we can’t get around.

      As the article mentioned, sometimes we can do this:

      To display the links to each of the individual posts pages, you use the function wp_link_pages which is customizable (as seen in the linked Codex article).

      But if the use case is as outlined in the gist, that’s what we have to roll with for now. At least it’s wrapped in a function, though :).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.