Subscribe to RSS Follow me on Twitter
Hi, I'm a web developer and blogger from Russia. My nickname is Dimox.
Sorry for my English, it's not my native. Read more about me and my blog.

WordPress Pagination Without a Plugin (WP-PageNavi alternative)

50611'
WordPress Pagination Without a Plugin (WP-PageNavi alternative)
Written by Dimox

Almost every blogger, who maintains a blog on WordPress, familiar with the WP-PageNavi plugin, which displays the pagination like on the image above.

But not everyone knows (including me until recently), what WordPress has a built-in function (starting from version 2.1) which implements virtually the same functional. And compared with the WP-PageNavi plugin this function very small in size.

Here’s what this function looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function wp_corenavi() {
  global $wp_query, $wp_rewrite;
  $pages = '';
  $max = $wp_query->max_num_pages;
  if (!$current = get_query_var('paged')) $current = 1;
  $a['base'] = str_replace(999999999, '%#%', get_pagenum_link(999999999));
  $a['total'] = $max;
  $a['current'] = $current;
 
  $total = 1; //1 - display the text "Page N of N", 0 - not display
  $a['mid_size'] = 5; //how many links to show on the left and right of the current
  $a['end_size'] = 1; //how many links to show in the beginning and end
  $a['prev_text'] = '« Previous'; //text of the "Previous page" link
  $a['next_text'] = 'Next »'; //text of the "Next page" link
 
  if ($max > 1) echo '<div class="navigation">';
  if ($total == 1 && $max > 1) $pages = '<span class="pages">Page ' . $current . ' of ' . $max . '</span>'."\r\n";
  echo $pages . paginate_links($a);
  if ($max > 1) echo '</div>';
}

This code must be placed in the functions.php file of your template. The function have just five options, they are separated by line breaks.

To show the navigation, paste the following code in your template:

<?php if (function_exists('wp_corenavi')) wp_corenavi(); ?>

As a result the HTML code will looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div class="navigation">
  <span class="pages">Page 7 of 27</span>
  <a class='prev page-numbers' href='http://site.name/page/6/'>&laquo; Previous</a>
  <a class='page-numbers' href='http://site.name/page/1/'>1</a>
  <span class='page-numbers dots'>...</span>
  <a class='page-numbers' href='http://site.name/page/4/'>4</a>
  <a class='page-numbers' href='http://site.name/page/5/'>5</a>
  <a class='page-numbers' href='http://site.name/page/6/'>6</a>
  <span class='page-numbers current'>7</span>
  <a class='page-numbers' href='http://site.name/page/8/'>8</a>
  <a class='page-numbers' href='http://site.name/page/9/'>9</a>
  <a class='page-numbers' href='http://site.name/page/10/'>10</a>
  <span class='page-numbers dots'>...</span>
  <a class='page-numbers' href='http://site.name/page/27/'>27</a>
  <a class='next page-numbers' href='http://site.name/page/8/'>Next &raquo;</a>
</div>

As you can see it contains all the necessary classes to stylize navigation with CSS (the current page, dots, links to previous and next page).

I’ve tested this function on WordPress 2.3.3 and 3.1 and also tested variants with active and inactive permalinks. Everything works as expected. I think it will work also in a new versions of WordPress.

Category: WordPress

Cоmmеnts (60):

  1. 1

    hey i was thinking; isnt there another alternative just like this but without being related to Wordpress (WP)????? thanks

  2. 3

    Very good solution. Thank you.

    And how i can remove the “page-numbers dots” and the first/last page link?

  3. 5

    It seems not to work in 3.2.1.

  4. 7

    Great function :)

    Many Thanks

  5. 8

    Great work, it’s exactly what I was searching for. Thank you.

    BTW, can I include this in a commercial wp theme?

  6. 10

    Great work..awesome..finally i found it thank you…..

  7. 11

    Hi, thanks for this. Is there a specific format to the loop that comes before the code?

  8. 13

    Thx for this.
    How can we separate the prev/next to the page numbers?

  9. 15

    I like your code, great idea
    Большое спасибо :)

  10. 16

    Hello,

    I was searching this, thanks for tutorial

  11. 17

    hey buddy………………you are doing great………

    i appreciate your work ..

  12. 18

    hey my friend, how can i edit it with css?

  13. 19

    I actually still need a fix for the search feature.
    Searching for two terms, like green dress, shows up correctly in the url for the first search page: green+dress.
    On the second search page the url shows “greendress,” returning a 404 page error.

    • 20

      The reason of this is not in my function. I have no such a problem.

    • 31

      To fix the problem, you should just change this line:

      if( !empty($wp_query->query_vars['s']) ) $a['add_args'] = array( ’s’ => get_query_var( ’s’ ) );

      to this:

      if( !empty($wp_query->query_vars['s']) ) $a['add_args'] = array( ’s’ => str_replace( ‘ ‘, ‘+’, str_replace( ‘%20′, ‘+’, get_query_var( ’s’ ) ) ) );

  14. 21

    Hi, thanks for your code :)…

    I’m creating a theme, but the first/last page links doesn’t show up…

    I only got something like this: http://oi54.tinypic.com/2e4d8qt.jpg

    How can I solve this?

  15. 23

    Can you please inform me what the style sheet is for “dots” ?

    Actually if possible to give css that would be easier to style this.

    Thanks!”

  16. 24

    OK, I figured it out.

    To alter the style sheets to dots

    .navigation span {

    }

  17. 25

    hi! this works great, but I have a small problem when it comes to category paging. it works wonderfully on my index with its respective paging (/?paged=2 or all the way up to /?paged=10)… HOWEVER, when I try to go to page 2 in a category (/?cat=5&paged=2) I’m getting a page not found message. any clues? thanks!

  18. 28

    Hi,
    is there any difference if i implement the code directly instead of using the WP PageNavi plugin?

  19. 30

    Hey Dimox,
    great work! But the pagination dont work on the search resulotion pages. Any idea?

    • 32

      I’ve updated the function. Now this problem is fixed.

      • 33

        Sorry but when i am change

        if( !empty($wp_query->query_vars['s']) ) $a['add_args'] = array( ’s’ => get_query_var( ’s’ ) );

        to this:

        if( !empty($wp_query->query_vars['s']) ) $a['add_args'] = array( ’s’ => str_replace( ‘ ‘, ‘+’, str_replace( ‘%20′, ‘+’, get_query_var( ’s’ ) ) ) );

        I get this…

        Parse error: syntax error, unexpected ‘-’, expecting ‘)’ in /www/htdocs/w009ad63/www/rockmartin/wordpress/wp-content/themes/rockmartin/functions.php on line 48

  20. 35

    I have added this to my functions.php and where I need it to appear in my templates. However, when I click on the links for page 2, 3, 4 etc. the URL changes, but I’m still on Page 1. What am I doing wrong?

  21. 37

    I am shocked to know about this as my simple philosophy is not to use any extension unless the functionality is provided in the system itself. I am going to discard page-navi extension right away and try this trick.

    You have found a new fan :)

    Thanks for this useful information.

  22. 38

    Right. Women like this … and I like this, too :D

  23. 39

    My theme so far has been a lot of use of ideas from you.

    However, if pleasure, may I request you to make a post about the Related Posts Thumbnails by Tags?

  24. 40

    I have a problem, the current page stay to “1″

  25. 42
    Jason Witt said:

    Do you know the file that this function is located in the WordPress Core? I can’t seem to find it

  26. 44

    Hello Dimox,

    Bit difficult to explain, but let’s say I’m on page 2 and wanted to get back to the homepage.
    Both clicking on ‘1′ or ‘previous’ return me to page 1 instead of the homepage.

    Do you know what’s causing this perhaps?

  27. 46

    I would like to add a ’show all’ link to this. I have added the code
    $a['show_all'] = 1; to your function but it does not add the link or change the output in anyway. How would I go about accomplishing this?

    • 47

      $a['show_all'] is not ’show all’ link. Read the description of this parameter in WordPress Codex:

      show_all
      (boolean) (optional) If set to True, then it will show all of the pages instead of a short list of the pages near the current page. By default, the ’show_all’ is set to false and controlled by the ‘end_size’ and ‘mid_size’ arguments.

  28. 48

    Privet Russian :)
    I’m just trying to use this function in my theme, however I have an issue. I want to start paging from the last page till first one.
    What I’m trying to say is, for example, it will start (assume I have 20 pages) from 20 and it will decrease to 1.
    Can we do that with this function?
    Regards,
    Doğan Kutbay

  29. 53

    Hello,
    I want to add multiple pages to one post. so will this be used for that also?

  30. 55

    very nice tutorail :D

Lеаvе а соmmеnt





Tag Cloud