We create beautiful & user-friendly websites and provide quality web development services worldwide.
Contact us to get started on your project.

Tutorial

How to Add WordPress Pagination without a Plugin [Enhanced]

Published by Veron Ang | 223


How to Add WordPress Pagination Without a Plugin

The default WordPress pagination only comes with the “Older posts” and “Newer posts” links at the bottom of the page when you want to navigate to the older entries. This works fine for sites with few entries, but isn’t very user-friendly for blogs and sites with dozens or even hundreds of pages worth of entries. To enable your visitors to quickly navigate your site and browse through your content from page to page, a list of numbered pages would be more useful, like Google’s search results.

Thankfully, there are many available WordPress pagination plugins that do just that. Among these, Lester Chan’s WP-PageNavi is possibly the most popular one.

But if you prefer to keep plugin overhead to a minimum, here’s a function you can use to add WordPress pagination without a plugin.

The code is provided by Kriesi and you can get his code and instructions. The pagination looks like this:





Enhanced Pagination by Sparklette Studio

We would like to provide an enhanced version of the pagination by introducing more useful information such as Page X of Y and make the arrows more intuitive, like this:




You can see a working example over at Sparklette.


Grab the code!

Here’s our sauce for the enhanced pagination, modified from Kriesi’s code.

  1. Add the following function to your functions.php file:

    function pagination($pages = '', $range = 4)
    {  
         $showitems = ($range * 2)+1;  
    
         global $paged;
         if(empty($paged)) $paged = 1;
    
         if($pages == '')
         {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
             if(!$pages)
             {
                 $pages = 1;
             }
         }   
    
         if(1 != $pages)
         {
             echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
             if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
             if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
    
             for ($i=1; $i <= $pages; $i++)
             {
                 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                 {
                     echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                 }
             }
    
             if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
             echo "</div>\n";
         }
    }
  2. To style it, add the following to your stylesheet (typically style.css).
    .pagination {
    clear:both;
    padding:20px 0;
    position:relative;
    font-size:11px;
    line-height:13px;
    }
    
    .pagination span, .pagination a {
    display:block;
    float:left;
    margin: 2px 2px 2px 0;
    padding:6px 9px 5px 9px;
    text-decoration:none;
    width:auto;
    color:#fff;
    background: #555;
    }
    
    .pagination a:hover{
    color:#fff;
    background: #3279BB;
    }
    
    .pagination .current{
    padding:6px 9px 5px 9px;
    background: #3279BB;
    color:#fff;
    }
  3. Finally, call the function in your theme (typically near the bottom of index.php or loop.php where it says "Older posts" or "Older entries"):
    <?php if (function_exists("pagination")) {
        pagination($additional_loop->max_num_pages);
    } ?>

Let me know if you find it useful!

Update: Also check out my new tutorial for adding and styling comment pagination.

Tags:

About the Author

Veron Ang is a , developer and WordPress expert at Sparklette Studio. She graduated with a bachelor's degree in computer science from the National University of Singapore. Engage her to work on your next project by sending her a message from the contact page or on Twitter.



223 comments

  • Awesome modification!

    I was wondering if this could be used with comments? And if so how could it be modified? I really love this solution and would rather not use a plugin in to achieve pagination with comments.

    Any help would be much appreciated

    • If you are using WordPress 2.7 or later, there is this function: paginate_comments_links.

    • Nice pagination …….

  • Thanks for the response! Could using that be modified to get the same look as post pagination function?

    • Yes, definitely. You can refer to the WordPress Codex for the styling of this tag. When I have time I’ll do up a tutorial for that as well.

  • Thanks that would be awesome :)

    I’m still learning my way with wordpress and always like improving the wordpress sites I create.

  • Another question if you don’t mind. I added twitterlink-comments plugin to the website i’m working on currently I have the twitter id written like {@twitterid} next to the comment author’s name but would like to have the twitter bird like how you have it . Is there a way to alter that with functions? I’m not sure what to alter I’ve tried in the plugin page and it came up with errors or not looked right like have the icon next to the twitterid

  • Excellent info!
    I would like to show a very large tabular data information and I want to paginate it using these technique. I will have to break the data into chunks before show it.
    ¿Could I use these ideas for a very long post?

    • That should not be a problem. You just have to make sure that the CSS classes match.

  • [...] ubiquitous numbered list, a la Google’s results page.More Useful WordPress Pagination SolutionsHow to add WordPress pagination without a plugin It is easier to navigate and the user instantly knows how many posts and pages are available. Its [...]

  • Hello everyone! I’m getting crazy with pagination…
    Everything is working except for the last link which wolud be supposed to display oldest post of the category but instead it does not and gives back a page containing “no article sorry” instead of last posts.
    There are 26 posts, 20 are perfectly paginated 10+10 and last 6 give the error. It seems that a page with 6 posts instead of 10 is annoying …. any idea? Please help! I used the code written in this page, thanks everyone!

  • Hi Veron,

    I am using your pagination code, but

    It’s not working on Firefox 3.6.12 I have installed Web developer toolbar, Fire bug

    & I am on Mac OSX 10.6.5

  • Sorry!!! It’s Working… :)

  • So simple and useful, worked perfectly, thanks a lot!!

  • How would I utilize this awesome function when using custom post types and taxonomies? I have a custom post type of “product_listing” and custom taxonomy of “product_cat” and am using rewrites to have my urls display like mysite.com/products/product-category/product-name/ which works fine. However, when trying to display a secondary page of content using my taxonomy.php file, I get re-routed to the index.php file and get a 404 error. The url still looks like mysite.com/products/product-category/page/2/

    Thanks!

    • You need to add loop remove $additional_loop and and your query $loop like this

      max_num_pages, $range = 2);} ?>

      • Same problem for me. How you do that??

      • I’m sorry can you explain that little more? in the functions.php the category.php?

  • As Ramsey I am also trying to use this for custom post types, however the pagination doesn’t seem to work at all with custom post types. I am using custom loops as well,

    Great work :)

  • Wow… I’ve been searching for a way to add pagination to my client work category pages and this actually worked. I can’t believe it. Veron, thanks for the great coding and styling (I’m a sucker for grays and blues).

    You’re a gem :)

  • hi,
    the pagination work really good, i had some problem to let it work in a page template, but i fix it now (http://wordpress.org/support/topic/pagination-dont-go-to-next-page?replies=5)

    I have one question : the code show only page x/x, but it dont show “next – previous – next – last” links?
    is that normal?
    thanks

    • I do not get the next, first, previous and last links either…

    • Have the same problem like Kamal and Trev with the prev and next buttons not showing up! Did somebody find a solution to this? Thanks for any suggestions…

    • same issue… would really like to have the NEXT/PREVIOUS links appear like in the example…..

    • same problem but just figured it out….. (in my case at least) its because the range was too high for the number of pages my blog had at the time. it seems they only appear if/when there are more pages than are set to show by default.

      function pagination($pages = '', $range = 4)
      change the 4 to a smaller number so that the number of pages you have is greater than the number of pages set to display by default, and the other links should appear!

      • yes you say right. add the $range then its appear.

        you can also add $range on Archive page like this

        max_num_pages, $range = 2);} ?>

  • FINALLY!!!!!!!!!!!!

    I have been searching for this for weeks….. and everyone always just said to use a plugin.
    I knew it had be a simple fix and thankfully you did too!

    THANKS SO MUCH! :D

  • I just discovered something – the pagination numbers at the bottom of my category pages only appear on the first page in IE and Chrome. There are none on the other pages. They all work in Firefox though. Any ideas?

    • Which version of IE are you using? I can see the pagination on all 3 pages of your “Client Work” category on IE8.

      • Ok, that was not there yesterday, lol. I have IE 8, too. I notice they are also there in Chrome now. Maybe it was a cache issue. I find that sometimes I have to delete everything and then run CCleaner and start the browser again to see the real changes. Pain in the rear.

        I’ve also found that with Twenty Ten it sometimes takes a while before changes will be reflected in the browser. I don’t know why. I’d love to find a better theme, but I don’t know enough about coding to get my site the way I want it, and themes can be complicated to tweak.

        Anyway, thanks for the reply, and for the great pagination code :)

        • Yup, it’s probably your WP Super Cache plugin. Each time I make changes to my theme, I have to remember to empty the plugin’s cache when I’m testing on a different browser (unless I’m already logged in as administrator).

          All credit goes to Kriesi for the pagination code. I merely tweaked it to look the way I want :)

          • How do you empty the cache on WP Super Cache? I don’t see any “empty” buttons.

  • @Doug Look for the “Delete Cache” option in your WP Super Cache Settings.

  • I don’t see anything that says “Delete Cache”.

    • Are you logged in as Administrator? You can access the “Delete Cache” two ways – the dropdown menu labeled as “New Post” in the WordPress header bar, or from the “Tester & Contents” tab in your WP Super Cache settings. Good luck!

  • Hello, world!

  • Hi, first of all thank you for the shared code :)

    I want to ask you if there is a way to remove /page/ from ../page/2 ( /page/3 , /page/4 ) ?

    Thanks,
    Ciprian

    • It’s probably not a great solution, but have you tried using .htaccess?

  • Absolutely perfect! Pagination worked great for me and the style is great. Thanks!

  • [...] you used this solution:http://sgwordpress.com/teaches/how-to-add-wordpress-pagination-without-a-plugin/ That is also Ok. Next we try to get the formatting for your second and next pages to look like your [...]

  • Excellent mod – being able to do this without a plugin was a great help!

  • Thanks for this. I’m having the same issue as a person above. I’m unable to get the “next” link to show. It shows EX: Page 1 of 2 | 1 | 2 | but no next link at the end. I’m using a child theme for twenty ten.

  • First of all I thank you for this wonderful post. It worked perfectly in blog posts page and I have one word, Awesome.

    However, I’m locally building a theme. I have a page with images in a gallery created by simple s since I don’t like using tables in pages.
    This gallery page has more than 50 images and I have 5 images per line and maximum 20 images in a page (I’m breaking the main page in to more pages using ).

    Now I have 3 pages and there’s a link at the bottom of each page that shows pagination. This is generated by found in page.php.

    I tried replacing that code with what you’ve provided here but then there’s no pagination. Do you think It is possible to have this option there too?

    Any help would be great.
    Thanks.

    • Are you using the wp_link_pages function? I imagine only the CSS needs to be amended.

  • Sorry. In my earlier comment, the code I pasted is wrong and here is the correct line of code

    '' . __( 'Pages:', 'customssa' ), 'after' => '' ) ); ?>

  • This worked perfectly. Thanks for the simple tutorial. I’m splitting a long WordPress page (not the posts page but a regular page) in to few pages using short code. This shows default pagination generated by
    ” . __( ‘Pages:’, ‘customssa’ ), ‘after’ => ” ) ); ?>

    do you know a way of writing a function to get the same pagination there as well.
    Thank you.

  • Hi Veron,

    Thank you so much for getting back after my very amateur comment posting. I thought you’d never reply instead delete my comments after messing up your blog. Sorry.

    Yes it’s the wp_link_pages. But then it’s not creating next previous links. When I replace this line with the code you’ve written, nothing comes up. I think I need to add some code to functions.php similar to what you’ve done with max_num_pages. Maybe I’m wrong.

    I hope you get what I mean.
    Thanks.

    • I notice that wp_link_pages only allows you to display either page numbers or Previous/Next links, not both at the same time. Hence the pagination would not look exactly like the one shown here. That said, we can style it to a certain extent. This is what I have done (the code should be added to the single.php template):

      <?php wp_link_pages('before=<div class="pagination">&after=</div>&link_before=<span class="inactive">&link_after=</span>'); ?>

      • Your answer was very helpful. Thanks once again Veron. It is adding the same styles to pagination. I’m sure I can develop the same functionality here too before I have enough pages to fill the width of the page. If I’m successful, I will not forget to publish it here.

  • Great article, thanks for sharing!

    @Kamal: I had to modify slightly the function to get the "next" and "previous" buttons to work (I also removed the "first" and "last" buttons). Try this:

    function pagination($pages = '', $range = 4)
    {
    $showitems = ($range * 2)+1;

    global $paged;
    if(empty($paged)) $paged = 1;

    if($pages == '')
    {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
    $pages = 1;
    }
    }

    if(1 != $pages)
    {
    echo "Page ".$paged." of ".$pages."";
    if($paged > 1) echo "<a href='".get_pagenum_link($paged – 1)."' rel="nofollow">< Previous</a>";

    for ($i=1; $i = $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
    {
    echo ($paged == $i)? "".$i."":"<a href='".get_pagenum_link($i)."' rel="nofollow">".$i."</a>";
    }
    }

    if ($paged < $pages) echo "<a>Next ></a>";
    echo "\n";
    }
    }

    • thanks a lot for the time you spent to help me,
      My regards

  • the pagination only works if you are using the global settings to limit the number of posts per page. lets say i have a gallery category that i want to limit from within the theme and leave the blog category to use the global posts per page, the pagination breaks. it will try to create the number of pages according to the global settings (default 10 per page)

    i have tried writing my loop in 2 different ways, but it doesnt change anything. i tried creating $post_count and using ( have_posts() && $post_count <= 9 ) and $post_count++; at the end of the loop.

    i also tried writing a new wp_query and use query( 'showposts=9' );

    is it possible to get it working with the first method so i dont have to create a secondary loop?

    • That’s not quite accurate. I have worked on a WordPress site recently using wp_query to display and limit the number of posts per page (not global settings). The pagination works fine if the proper parameters are defined. For example, the query I used in my theme is $wp_query->query('caller_get_posts=1&showposts=10&paged='.$paged);.

      • What I have right now actually is let’s say…

        if ( in_category( ‘1’ ) ) { $post_count = 1;
        while ( have_posts() && $post_count <= 9 ) : the_post();

        stuff…

        $post_count++; endwhile;
        endif;

        How should I modify the code to fix it? I'm already using the counter for another purpose, so I just reused it for limiting the post count.

        • } not endif;

        • Assuming you are displaying the posts within a specific category, your if statement should be is_category not in_category.

          I rewrote your code to the following:


          <?php if(is_category('1')) {
          $wp_query = new WP_Query();
          $wp_query->query('showposts=9&paged='.$paged);
          while ($wp_query->have_posts()) : $wp_query->the_post();
          stuff...
          endwhile;
          if (function_exists("pagination")) {
          pagination($additional_loop->max_num_pages);
          }
          } ?>

          • so using $wp_query in the loop.php won’t make it less efficient?

      • Thanks so much for that query code Veron! Works perfectly!!!

  • I would place that $wp_query in the archive.php or category.php file. It would apply only to your category ID 1. Apart from this method, I don’t know how else to control pagination at the theme level.

    • actually im going to have several gallery categories limited to 9 posts.. the example up top was just an example.. i left cat 1 alone and used else statement for the galleries..

      i changed part of the code up top to query( ‘cat=’ . get_query_var(‘cat’) . ‘&showposts=9&paged=’ . $paged ); and it still displays 18 posts on 2 pages and 1 missing out of 19 posts..

      so i guess the only way is take the gallery categories out of loop.php?

    • Sorry I keep coming back here, but I tried following your advice directly and it still doesn’t work.

      To make sure nothing else is causing the problems, I went into category.php and replaced get_template_part with:


      query('showposts=9&paged='.$paged);
      while ($wp_query->have_posts()) : $wp_query->the_post();
      endwhile;
      if (function_exists("pagination")) {
      pagination($additional_loop->max_num_pages);
      }
      } ?>

      19 posts in cat 5. For some reason it now has 20 pages and page 3+ returns 404.

  • [...] Here are some articles are very helpful: 5 Excellent WordPress Pagination Solutions How to add WordPress Pagination without plugin [...]

  • Wonderful solution. Thank you.

  • I´m trying to use your code with custom post types, but is not working.

    I´m also following this tutorial http://boring-group.com/blog/2010/05/28/wordpress-query_posts-with-custom-post_types-and-paged-solved/

    Someone have a tip to or know how to solve this?

  • Hi Veron,

    I wanted to email you specifically, but I couldn’t find your email addy on the site. So I’m going to ask this here instead. I got that whole pagination code working on my gallery pages, but now I would like to have the pagination on my Testimonials page. I’m just wondering how I would get it to work there. Is that possible?

    Also, do you guys code WordPress themes for people? I’ve been looking for some WP coders to have on hand for when clients want whole WP sites built.

    Thanks :)

    • Hi Doug, I had a look at your site. It seems that your Testimonials page is a static Page. The pagination only works for Posts, so in this case you can perhaps try creating a new Category called Testimonials, then create a new Post for each testimonial. Basically the structure would be like your gallery pages. Does that make sense?

      And yes, we specialise in building WordPress sites. You can see some of our works in the portfolio. Drop me a mail any time at veron[at]sparklette[dot]net. Thank you.

  • Hmm, that sounds like too much trouble than it’s worth. I hate WP sometimes. I long for having just a plain old HTML web site with no CMS garbage.

    • If you just wanna split that single Testimonials page into a few pages, you may be able to use the native WordPress wp_link_pages function. It works for Posts, but I have yet to try it with Pages. My guess is that it should work similarly.

  • Show it – http://www.anuariodacomunicacao.com.br/tipo/agencias-de-propagandas

    The pagination in Custom Post Type, but not working when click in pagination. Solution?

  • Hi! Great ‘hack’, thank you so much! :)

    It works fine on my archives page but I’ve got 2 problems:

    1/ How can I center? I tried everything but I can’t get the pagination in the middle of the page…

    2/ I used the enhanced pagination code you provide but I don’t see the ‘Last’ and ‘Previous’ links. They don’t appear even when my archives shows 7 or 8 pages.

    Thanks a lot for your help! :)

    • I’ll quickly answer your first question. To auto-centre the pagination, you’ll need to give it a width and margin. For example, you can add this CSS to the .pagination class:

      width: 400px;
      margin: 0 auto;

  • Hi Veron, thanks for your reply! I did that but unfortunately, it didn’t work either… Weird, isn’t it? I honestly tried EVERYTHING :)

    As for my second question, I looked at the code again and I still don’t understand why ‘Last’ and ‘Previous’ links don’t show.

    I must have missed something somewhere, I’m lost O_o

    • Perhaps you can leave a link so the rest of us can have a look.

  • Thanks! I’ve been trying out different plugins with mixed (mostly poor) results, but this worked like a treat on my theme (and without the need for yet another plug-in!)

    Awesome.

    • It’s great isn’t it? Where possible, we should try to minimise the use of plugins.

      • Yeah. Better coded into the theme.
        Here is a link to my site.
        http://www.sutmundo.com

        Again, thanks for the great mod.

  • Hi there, i’m trying to find a way to comment to Kreisi’s blog but no luck. And since i find you, you might wanna shed me some light. Im creating a custom author page which displays his list of posts….here’s the code

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; //get the paged posts
    $authloop = new WP_Query("ID=$uid&paged=$paged"); // handles the new query
    $ppp = 5; //my custom number of post to appear
    $uid = $curauth->ID; //get the user's ID of the author
    $args = array(
    'numberposts' => $ppp,
    'author' => $uid,
    'paged' => $paged

    );
    $authorposts = get_posts($args);

    //loop goes here to show the first 5 posts

    authorpagination($authloop->max_num_pages); //function of kreisi

    The code is working. However, the pagination get’s the total number of page overall and not for the specific author . For example, the blog has overall post of 30 and user Charles has 10 posts, so it should be showing 2 parts of page…..but mine is showing upto page 6….

    • Hi Ven, this may not seem like an answer, but why aren’t you using the default WordPress author templates? You should be able to include the working pagination in there easily.

      • nope i’m not, i’m creating this as i’m making my wordpress as multi-author site. Wherein, the authors’ pages will have a new look. :) i’ll send you the screenshot on your fb page..so you’ll get what i mean

        • Are you inserting this code into the author.php theme file? It seems as though you may have placed the code within index.php.

          • it is of course on author.php as i told on my earlier post i’m creating this for my custom author page.

  • Thanks for both post..

  • Hey this is great Veron! I’m wondering if you can help me… I’m looking to paginate, not the loop, but rather, the contents of a post, dividing it into x sections with pagination at the foot of each. Do you know if this is related or similar to the method you’re sharing here?

  • Please HELP!!!!!!!!! i have put the script on 3.1.2. wordpress fucntions and all is good except it causes a problem on wp-admin, the post section, FEATURED IMAGE do not save images no more.

    Excelent tool, but i need help please!!!

  • Many thanks! I just implemented the code on 3p and it looks great

  • [...] sumber: sparklette studio [...]

  • Hey Veron,
    just want to say “Thank You” for youre really helpfull tutorial!
    Best wishes from Germany.

  • hi, thanks, its an awesome post, i just found it very interesting and grabbed the code for doing my work more classy, thanks for the great pagination.

  • Awesome! One plugin less. Thanks for sharing so well explained.

  • Hi, any idea on how to reduce the number of options shown. Right now it displays 9 numbered items, I want to display 7 only

    • Try modifying $range = 4 in the first line.

  • very nice :D i just implemented this into my blog. it works awesomely~

  • Thank you very much for sharing the code. I have a question. How do you center the pagination so that is it always in the middle of the page. I have spent a few hours but can not figure it out.

    • In the pagination class, remove the clear:both;, add a width and margin: 0 auto.

  • Awesome! I search a good pagination for my templates for a long time and this is the first good. Thanks. :D

  • Took me a second to get it working in the functions.php file. I had to copy and paste the code into a separate notepad file, then copy that into my functions.php. There must have been some formatting thing that I missed.

    In any case, great tutorial! This works really well and helps with plugin overhead.

    Thanks!!!

  • I am having trouble getting this to work completely. When I use it the links show up but when I click on 2nd page it just reloads the same posts that were on the first page. Any ideas what may be causing this and how to fix it?

    • I finally figured out what was happening. Since the page I was trying to paginate was a static page it was messing up the pagination. To get around this I had to change my query to this:

      $paged = (get_query_var(‘page’)) ? get_query_var(‘page’) : 1;
      $temp = $wp_query;
      $wp_query = null;
      $wp_query = new WP_Query();

      $wp_query->query(‘orderby=date&order=DESC&paged=’ . $paged);

      if($wp_query->have_posts()) :

      while($wp_query->have_posts()) : $wp_query->the_post();

      Hope this helps anyone else with this problem.

      • you just saved my day!

  • [...] którą sprawdziłem i wdrożyłem na tym blogu została opisana w artykule na blogu Sparklette Studio i opiera się na edycji plików szablonu [...]

  • Great Post!

    Thanks for the information!

  • This is GREAT.
    Very nice to have in my code library, thanks a lot!

  • Hello,

    I’m curious if you have or can point me to pagination solution that will work with twenty-eleven theme? Any advice would be appreciated beyond groovy. Thank you, and I love your sparklette site.

  • Thanks! Very helpful article!

  • Hi Veron,

    I’ve tried to use a plugin for pagination of my index page. It somehow messed up my (Thesis) sidebar and I had to find an other solution.

    Your script did the trick and all I now have left is to edit the css to fit my layout.

    Thank you so much for providing this information :)

    Best regards,
    Michael

  • how to add pagination for wordpress?

  • [...] How to add WordPress pagination without a plugin [...]

  • thank you very much. works like a charm. but here is the question. it makes the permalinks like that; domain.com/page/1

    how can i change the /page/ slug to something else?

    thank you.

  • Hi, great post & many thanks!

    Like a couple of others, my ‘Next’ & ‘Previous’ links are not displaying (although the numbers work fine).

    I use Twenty Ten and so use the ‘wp_link_pages’ function. Is there a way I ca replace this function with another that will display both numbers & next/previous?

    Thanks in advance.

  • thanks for this post, i couldn’t figure out how to fix the spacing in the old version

  • Hi Veron,
    Your tutorial looks so simple and from the volume of successful feedback it is!
    Therefore I am clearly doing something wrong.

    Intro: I’m a newbie to WP, with beginner – intermediate skill @ PHP.

    I’ve pasted the code of my page below.
    It is a custom page, that filters on categories.
    I’ve also tested WP-Navigation without luck, so it must be something to do with my php function to filter on the categories.
    Any help in this regard will be greatly appreciated.

    ID, ‘category’, true);
    }
    if ($category) {
    $cat = get_cat_ID($category);
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $post_per_page = 5; // -1 shows all posts
    $do_not_show_stickies = 1; // 0 to show stickies
    $args=array(
    ‘category__in’ => array($cat),
    ‘orderby’ => ‘date’,
    ‘order’ => ‘DESC’,
    ‘paged’ => $paged,
    ‘posts_per_page’ => $post_per_page,
    ‘caller_get_posts’ => $do_not_show_stickies
    );
    $temp = $wp_query; // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    ?>

    have_posts() ) : $wp_query->the_post(); ?>

    <a href="” title=”” rel=”bookmark”>
    Category:   |  Posted:

    <a href="”>

    <a href="”>
    <img width="150" height="150" src="/images/wpota-dpi.png” class=”attachment-post-thumbnail wp-post-image” alt=”WPOTA” title=”WPOTA” />

    <?php the_content('… continue »’); ?>

    <?php _e('Sorry, but no results were found.Perhaps searching will help find a related post.’); ?>

    max_num_pages);
    } ?>

  • [...] some commonly used WordPress plugins can be easily replaced by adding new functions to your theme:How to Add WordPress Pagination without a Plugin [Enhanced]How to Add WordPress Paged Comments without a Plugin (plus CSS Styling)How to Display Most Commented [...]

  • Hi!
    Thank you for shared code!

  • Hi, I hope it’s not to late to get support with this : )

    I’m trying to use this for a multiple functionalilty plug-in for a certain project I’m working on, but I’m a little new to this. I have the coding (working) placed in a .php file, and I’m trying to include the CSS on a seperate CSS file how would I go about doing this? I’m struggling with it : /

    I had to rename the function to “wp_pagenavi” since it’s going to be replacing the auto-called plug-in a theme. Although, I don’t think that matters?

    • Nevermind, I figured it out. Thanks for the code!

  • [...] with the same name.Sparklette Studio: Provided the coding for the “Simple Pagination”. (Original article)Dig WP: Provided the codes for “Insert jQuery” & “Threaded Comments” [...]

  • I love your function! :D

    But I’ve got one problem within my category.php. I’ve got 3 pages, the first and second work perfect, but if I click on the third it gives me a 404. I also use the pagination in my page.php with the same code and don’t have any problems with it.

    Maybe you can help me. Here’s my code (I deleted the from the code, cuz it didn’t show well in the preview):

    ?php foreach((get_the_category()) as $category) { echo $category->cat_name . ‘ ‘; }?

    ?php
    global $wp_query, $meta ,$paged;
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $temp = $wp_query;
    $args=array(‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => 5, ‘showposts’=> 5, ‘paged’ => $paged );
    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ):
    while ($my_query->have_posts()) : $my_query->the_post(); ?

    … content …

    ?php endwhile; ?

    ?php if (function_exists(“pagination”)) {
    pagination($my_query->max_num_pages);
    } ?

    ?php endif;?
    ?php $wp_query = null; $wp_query = $temp; ?

  • Thanks. I was wondering how people did that on their blogs.

  • Hi, Great code idd worked like a charm!

    I personally still have one issue, i would like to use a different layout/template for the /page/2, pages after the first which is the homepage. Any ideas on how i could solve that?

  • Veron, I’m sorry I still can not apply the “enhanced pagination, modified from Kriesi’s code” on my weblog. I use wordpress-3.2.1 with the themes “house_listing_for_sale_bue049″ and have followed the procedure: “1. Add the Following function to your functions.php file:, 2. To style it, add the Following to your stylesheet (typically style.css) .3. Finally, call the function in your theme (typically near the bottom of index.php or loop.php where it says “Older posts” or “Older entries”): “Both the” posts “or the” pages “have not appear “pagination”. How do I bring up the “pagination” in posts and in the pages? Can you explain in more detail presumably because I do not understand programming. Thank you.

  • Hi,
    Just wondering how this could be adapted to use with a collection of pages in a “Before and After” gallery. Each page is a new before and after image collection and I would like to be able to add a new page and have the 1 of 49 dynamically become 1 of 50 so on and so forth.

    Is this possible or do you know someone who could code if for me?

    Thanks
    Greg

  • Thanks buddy, this is what i’m looking for

  • Thanks man. Have integrated the same. Appreciate your help.
    I hate plugins btw.. :)
    Keep posting.
    Cheers…

  • Dude THANK YOU.

    You don’t even wanna know what my code looked like before I found this solution…

  • It Work for me..
    This tutorial very help me
    Thanks for Share…

  • I’d like to know what happen to the pagination on the single post if it were a query_posts on a certain category in a particular loop. Thank you

  • I’ve tried implementing the code but the links just keep reloading the same page.

    Here is my function:

    function pagination($pages = ”, $range = 4)
    {
    $showitems = ($range * 2)+1;

    global $paged;
    if(empty($paged)) $paged = 1;

    if($pages == ”)
    {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
    $pages = 1;
    }
    }

    if(1 != $pages)
    {
    echo “Page “.$paged.” of “.$pages.””;
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "« First“;
    if($paged > 1 && $showitems < $pages) echo "‹ Previous“;

    for ($i=1; $i = $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
    {
    echo ($paged == $i)? "”.$i.””:”“.$i.”“;
    }
    }

    if ($paged < $pages && $showitems < $pages) echo "Next ›“;
    if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "Last »“;
    echo “\n”;
    }
    }

    and here is the loop with the call to the function:

    <a href="”>

    <a href="”>post_title; /* or you can use get_the_title() */
    $getlength = strlen($thetitle);
    $thelength = 31;
    echo substr($thetitle, 0, $thelength);
    if ($getlength > $thelength) echo “…”;
    ?>

    max_num_pages);
    } ?>

    What is causing the links to reload the same page?

    • I have the same problem… Do you resolve it ?
      Thank you

  • Great tips and very helpful!

  • Is there a way to set different loops instead of max_number?
    Something like:
    if (function_exists("pagination")) { pagination($additional_loop->post_per_page=3)

  • Nevermind, I answered my own question.

    For custom pagination, just use query_posts( $query_string . '&posts_per_page=3' ); from
    http://codex.wordpress.org/Function_Reference/query_posts.

    Thanks!

  • I get a PARSE ERROR: syntax error, unexpected T_STRING

    on the following line, inside de functions.php file

    $showitems = ($range * 2)+1;

    Any idea of what it may be wrong?

  • Just curious…If I have a post that is say 5,000 words long (a big post, I know) I really want to paginate long posts into multiple pages without have to use

    Would this work for that or could it be easily modified?

  • [...] NumbersNo need to explain this one. Check out this awesome tutorial that explains it all: How to Add WordPress Pagination Without a Plugin.Tag CloudThis is an easy one. To create a tag cloud (which is what you see on the bottom right of [...]

  • [...] How to add WordPress pagination without a plugin [...]

  • Thanks great snip,

    I would like to add my two pence…
    For all those having trouble displaying the next and previous links I had to update your code a little. I changed the beginning to this:

    function pagination($pages = '', $range = 1)
    {
    $showitems = ($range * 1)+1;

    Now It appears correctly.

    Thanks.

  • Hello,
    Love this hack ! I removed WP-Pagenvi to use your code and wow it works but no in category.php

    Works with my Home page with those urls :

    mysite.com/page/1
    mysite.com/page/2

    But not with my categories (category.php) I can see the pagination but I’ve got 404 error :

    mysite.com/blabla/page/1
    mysite.com/blabla/page/2

    I’ve got ideas ?
    Thanks.

  • It works fine!!! Thanks!

  • Works like a charm :) This is awesome and veryyyy usefull thank you for the help for the magic!

  • thanks a ton working for me thanks again .. :)

  • This code is cool, but what I love the most with plugins is that they get updated when required.

  • [...] you also run a blog. The solution is to add pagination to the search results. Check out this link http://sgwordpress.com/teaches/how-to-add-wordpress-pagination-without-a-plugin/ I think it is a very nice solution for what you want, and it looks good in the theme as [...]

  • worked perfect for me, Thanks.

  • Thank you very much!

  • Thank you, this was very easy. I grabbed the code, pasted it in the templates and it worked. Very smooth!

  • Outstanding. Thanks a ton for this one. Works like a charm.

  • thanks so much, this gave me too much

  • Thank you!!!

  • [...] The code is provided by Kriesi and you can get his code and instructions. The pagination looks like this: 12345›» [...]

  • I am using your code but it seems to load slow when I have over 10k+ posts even I have set it to paginate every 50 posts. It gives 131 queries in 6.730 seconds. Is there a better way to optimize the code to load faster?

  • thats too good….

    your edit Kriesi

  • I am newer in wordpress, I do all the code above but i can view the pagination link

  • Hi there, thanks for this great tip – I followed all the steps and want to add it to my search.php page so there is pagination instead of the “Older Posts” option – but I’m having trouble getting it working. Could you help me by telling me where exactly I should be placing this code in my search.php to see the pagination?

    Thanks for your time!

  • Thanks, this is wonder for me. I used this pagination functionality it is working fine

  • Easy to implement and works like a charm! Thanks!

  • Wow! Totally and instantly useful. Tested it out with WordPress 3.4.2 using iThemes Builder. Fantastic!

  • [...] via:How to Add WordPress Pagination without a Plugin [Enhanced] [...]

  • Cheers for this, this was exactly what I was looking for. I had to redesign a posts page on a client site, but wasn’t sure how to pagination would work.

    Even the default CSS sits very well with the site! Great job.

    http://www.blulift.ie/category/news/

  • thanks for this tutorial, this pagination looks great instead of what i found elsewhere

  • Amazing bit of code!

    Gave my website a great finising touch :D

  • Thanks a ton, i was search about this :-)

  • Hi,
    I had your pagination code working fine. I then updated to WordPress 3.5.1. I updated theme plugins etc. All seemed fine thenI noticed the blog page pagination. When you load the blog http://babasouk.ca/blog/ it works fine 1 of 13.
    But click page 2 and you get 2 of 2
    Click last or 3 you get a list product post text?

    Does this give any clues as to what might be wrong?

    • some more information. When It first displays 1 of 13 that is the correct number of pages for the number of blog posts.
      When you go to page 2 you get blog posts.
      Any page above 2 you get text from custom post type products which should not be involved at all.

  • Hi, I have a little problem.

    I integrated your code as explained. it works perfectly in against the bottom of page three lines appeared and I can not remove them. how do I remove them?

    Best regards
    Stormcorporation-esport

  • Help! I was trying to do this but ended up with this error message:

    Parse error: syntax error, unexpected ‘<' in … /public_html/wp-content/themes/platform/functions.php on line 64

    I'm sorry I'm a techno idiot and probably shouldn't have tried this, but I installed wp page navi which didn't work so I thought I might as well try this.

    What have I done wrong? Now I can't even access my site, pleeease help….!

  • I’ve managed to access my website and have deactivated my theme, so am safe for now. Maybe I should stick to plugins instead. Thanks for the tutorial, I hope I can use your tips one day!

  • How can use this in custom query? here my code. thanks

    query('posts_per_page=3&category_name=news');
    while($preNewsPosts->have_posts()): $preNewsPosts->the_post();
    ?>
    <a href="" class="atitle" >

  • I added the your code to my function.php file but then it caused my whole site not not load I could not even access the login to wordpress. Why is this happening thought your method would work with any wordpress I am using version 3.5.1

  • this helped me alot thanx alot…

  • if i short according to cat_id or CatName so what parameter are pass in array of
    $args=array(‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => 5, ‘showposts’=> 5, ‘paged’ => $paged );

    Please Explain as soon as .

    Thanks

    Dileep Maurya

  • [...] 이미지처럼 저렇게 나온다. 먼저 참고한 글은 영문이긴 한데.. 이 포스트이다. 플러그인을 찾아도 되는데.. 이상하게 EXIF 부분도 그렇고 직접 코드를 [...]

  • Cool pagination

  • hi thanx

  • [...] y pie de página. Para añadir una paginación de las entradas, te recomiendo este post sobre cómo crear la paginación de las entradas en WordPress. Aunque esté en inglés, como es sólo código es fácil de entender. En cualquier caso, si [...]

  • thanks

  • Hi Veron
    Thanks for this code. It works perfectly.
    So Ihave a question ^^
    I would center the element and I followed the tip that is on page 2 and page 3 : “remove the clear:both;, add a width and margin: 0 auto”
    But I’m trying to center the element by changing the value “widht” but I can’t do…
    Have you an other tip for center the element of pagination definitively ?
    Regards

  • I am having trouble to display the next page, how do i do this? Some one please help me~

  • Thanks a ton! Saved a lot of time and got a neat solution :)

  • Hi,
    thanks for the code works good, but doesn’t work on the custom post types. mainly the problem is with the total count.
    for example there are 5 items to show on each page , total are 10 , so the total page count should be 2, but instead it is showing page counts into 100. whcih i am understanding it is acutally showing the page count for the normal post types. in pagination.
    is there a work around for that?
    thanks

  • Very Very Usefull

  • [...] Add wordpress Pagination without a Plugin [...]

  • Here’s an easier and more effective way to add single posts nav links which link all posts in all categories

    add_action( 'genesis_entry_footer', 'category_post_nav', 5 );
    function category_post_nav() {
    if( !is_single() )
    return;

    previous_post_link('%link', '<< Previous Post in Category', TRUE);
    next_post_link('%link', 'Next Post in Category >>', TRUE);
    }

  • Thanks so much for this! Unfortunately I am stuck at Step 3 because I am using Genesis with a child theme where neither of those two files exist for editing. Can you please instruct where I would place the Step 3 call to get this to display on my posts?

  • Can’t believe this worked out of the box on my custom theme. So simple. Thanks a lot!

  • [...] with the latest posts, sidebar and footer. To add a page of entries, I recommend this post on how to create pagination in WordPress entries .Although it is in English, as it is only easy to understand code. In any case, if you download [...]

  • This is so useful, but does the pagination disappear if there is no next page or page 2? Anyway i’ll try your code, hope this answers my questions. Thanks a lot for sharing! Bless you!

  • […] How to add WordPress pagination without a plugin […]

  • thanks, i use it on my blog :)

  • I am not sure what i am doing wrong but i am getting same post on both page one and page 2 :( please help

  • first of all, thanks a lot for share this code.
    I’m using custom post type and this code didn’t work for me BUT I googled and found the following code to helpe me make my code works:

    $paged, ‘posts_per_page’=>(the number you wish), ‘post_type’=>’you_custom_post_type’, ‘offset’ => $offset);
    ?>

    **without $paged and $offset vars it didn’t work for me.

    font: http://boring-group.com/blog/2010/05/28/wordpress-query_posts-with-custom-post_types-and-paged-solved/

    • I don’t know what happend on my comment submit; the code was broked. This is the correct code:

      $paged, ‘posts_per_page’=>(the number you wish), ‘post_type’=>’you_custom_post_type’, ‘offset’ => $offset);
      ?>

      • the submitting error happened again. the $offset declaration:

        $offset = ( 5 * $paged ) – 5;

  • I don´t understand where can I put the last code, the tutorial says:

    Finally, call the function in your theme (typically near the bottom of index.php or loop.php where it says “Older posts” or “Older entries”):

    But i want to know where put the following code exactly

    max_num_pages);
    } ?>

    My theme is: http://wordpress.org/themes/raindrops

    My page is: http://www.falsalibertad.com/

  • I tried this solution, thanks, but i don’t know why not working in home page.
    When I click page 2, nothing happens, it fails to go on two.
    I use WP 3.5.

    Any clue, please?

    Thanks!

  • […] Selamat mencoba, semoga tulisan Membuat Paginasi WordPress Tanpa Plugin bermanfaat bagi anda. Tulisan aslinya bisa dilihat di sini. […]

  • […] used the scripts share by http://sgwordpress.com/ where in it is enhanced and still compatible in the latest wordpress […]

  • […] PHPやCSSがわかる方には、こちらの方法がおすすめです。 以下のサイト(英語)を参考に賢威6.1用にアレンジしています。 How to Add WordPress Pagination without a Plugin [Enhanced] | Sparklette Studio […]

  • thanks for tutorial.. because beatiful sharee

  • Hi,

    I’m not a developer and own wordpress site and tried the pagination past one month but i can’t do it. Today i have done this as per your site suggestion…

    Thanks for posting nice post.

    Keep on good post.

  • There is a problem with this advanced page navigation, it is showing more pages than what there is.

    Example 1: it shows there is 1000 pages but in fact there is only 950 pages.

    Example 2: it show 8 pages but only 6 pages exists.

    The last button would end up at 404 and all pages before it that don’t exist.

    Link: http://blog.codecentral.org/downloads/page/6/

    Anyone found a solution for this error?

  • I’d like to make use of the “…” after the first few page number links as you’ve done on your comment pagination. How would I accomplish that?

    Thanks in advance!

  • Thanks! WP-Pagenavi not working on my theme but this works perfect.

    Best Regards!

  • Is there a way to do this for the wp_page_links code?

    I want to set up this system for when I use but have not been able to find a comprehensive tutorial to do so. Thank you!

  • Is there a way to do this for the wp_link_pages code?

    I want to set up this system for when I use but have not been able to find a comprehensive tutorial to do so. Thank you!

  • Hi dear, It is working in all pages. But not work in single page. Please help me….

  • Hi, thank you for this tutorial!

    But I’m still a bit confused on how to implement this with only a select group of pages. (I don’t want to use it for my blog…. I already have a pagination that works well for the blog.)

    I’m trying to link together a series of about 5 pages. So where do I put the code to call those pages to link together? (Sorry, I’m not an expert on coding!)

    Thank you!

    lily

  • Hi again. I gave up on trying to do a pagination with static pages…. so I created a series of posts under a particular category.

    But I still could use some help, please! I don’t want to change the pagination on my site overall. I ONLY want to use this with one specific category.

    What code do I use, and where do I put it so it doesn’t affect all my posts, but only one category?

    Thank you!!

  • Excellent Veron,
    This was a tremendous help. I had problem with having both a to z index and pagination on my custom template. Your method solved my problem.
    Yuo are menna from heaven,
    Many thanks,

  • Perfect!
    I have found another perfect solution to extend this feature. It allows you add a title, description and image in pagination button. Check this free WordPress plugin out: http://wordpress.org/plugins/advanced-content-pagination/

  • […] without plugin:拡張版」 が元ネタです。 こちらのサイトでも、実は、 「How to Add WordPress Pagination Without a Plugin [Enhanced]」 が元ネタのようです (笑) が、きちんと元ネタページを明記され、 […]

  • Hi there,

    I have already a website that I am adding manually the pagination simple code on the place which I want to split the post in, now your code seems very advanced which I really would love to use it but my question is how to make it manual implimintation just like I stated previuosly that I need to assign the location of where the post should be split … so will your code do the same job for me?

    I mean if I used your code will it look automatically like yours using my own old codes? or it will assign its own locations automatically?

    Kindly advice..

    thanks

  • Really Simple and useful, thumbs up (Y)

  • sir i am using posts of same category on a page. i want to display 5 post on page at a time to go to next posts i want to use pagination on the end of page……can u send me the code plz………



Leave a comment