An Easy Way To Check if a Post is Paginated

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.

5 Comments

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.

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 :).

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

Leave a Reply

Name and email address are required. Your email address will not be published.

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>