0

After ending The Loop, I simply have:

echo paginate_links(array(
    'mid_size'           => 3,
    'prev_text'          => __( 'Previous posts' ),
    'next_text'          => __( 'Next posts' )
));

However, pagination is displayed only when the number of pages is greater than 1. In other words, if I have 1 post and in the Wordpress dashboard set "Settings -> Reading -> Blog pages show at most 1 post" then pagination isn't displayed, if I add a posts, it will be. If I then increase the dashboard setting to 2, the pagination disappears again.

You may wonder what pagination I would expect to see if I have only one page. I would expect to see the current page displayed, i.e. 1. Not having this spoils my page layout.

Is this behaviour normal? It seems odd that it would be, given that category pages for new categories with only a single post therefore show no pagination.

If this behaviour is normal, how do I figure out that there is only one page to display so that I can insert a current page 1 into my page to fix my layout?

1

Behavior

The behavior of removing pagination when there are fewer than 2 pages of items to display is hard-coded and intentional. The paginate_links() Codex entry details that the function's primary purpose is to display pagination links for archive posts. An archive being a collection of items, it could be argued that a single item by itself does not constitute an archive.

At the same time I understand your motivation - enumerating singular items seems a redundant task at first glance, but it certainly has it's applications in design.

Still, though, I must add that I feel it presents the end-user with data of little to no relevance to their interests - most visitors don't tend to care that they're on page 1 of 1. And if an archive is only ever to contain one item, then it would be far stranger to display pagination implying pages of items that will never exist.


Background

This behavior can be identified in the source for the paginate_links() function at line 2628, wherein the function returns early if there are fewer than 2 pages of content.


Solutions

  • My personal approach would be to place the pagination in a <div> of determinate height and maintain the function's hard-coded behavior. In this manner, pagination is still displayed only where necessary, yet it's absence will not muck up the rest of your layout.

  • If it is absolutely necessary that the page number be displayed when there's only one page, Andrei's suggestion to use is_paged() to conditionally display an alternative to the pagination is the way to go.

  • Write a custom function to output pagination links in the manner you desire

  • It is possible to create a hack that will achieve the desired effect by spoofing the number pages of available before the call to paginate_links(), then removing the links to non-existent pages from the function's return value or via the paginate_links filter hook - however, doing so likely takes much more time, effort, and code than other solutions, and will likely be broken by a core update sometime down the road.

  • Thank you for such a clear explanation, I now understand. In my layout, I can't simply use a fixed-height div, but I can use is_paged() to alter the layout appropriately. – Ninjakannon Feb 3 '15 at 3:21
1

The default pagination of blogs in WordPress is done by Older posts & Newer posts links/buttons.

When you're on first page, you can't/shouldn't have Newer posts, as there are none. If present, the button would have to be either disabled or misleading. Same goes for the Older posts when you're on last page. So, if you are on first and last page of your posts they should both be missing or disabled.

However, if you got to change this for whatever reasons, the conditional you're looking for is is_paged().

This would be a typical display, using the paginate_links() from your question:

    if ( is_paged() ) {
        echo paginate_links(array(
           'mid_size'           => 3,
           'prev_text'          => __( 'Previous posts' ),
           'next_text'          => __( 'Next posts' )
        ));
    } else {
        echo '<a href="javascript:history.go(0)">Page 1</a>';
    }

You will have to wrap the link in the same parents as the pagination links and give it the classes of those elements and also make it active. Study the elements of your pagination links for this.

  • I'm not too bothered by not showing buttons to older and newer posts. However, the pagination I am using shows the current page in the middle when there are multiple pages. I would like it simply to display a 1, indicating that you are on the 1 and only page. It would also be nice, in fact, if it displayed previous and next navigation links without anchor tags. Also, the documentation says that is_paged() is only true when the current page number is greater than one, which it won't be in this case. So I don't think it does apply, or am I missing something? – Ninjakannon Feb 2 '15 at 23:41
  • 1
    @Ninjakannon, @Andrei is suggesting that you check if is_paged() is false, and presenting markup that displays "Page 1" instead of calling paginate_links(). If is_paged() is true, paginate_links() would be called normally. – bosco Feb 2 '15 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.