WordPress Pagination Again


Some days ago, Frank wrote a post about Custom Pagination without WordPress Plugins. Jay comment, that something like this already exists (since Version 2.1). We don't know everything. ๐Ÿ˜‰

How do you get it in your template? The best is, if you have a function, which you can use in several templates.

function my_paginate_links() {
    global $wp_rewrite, $wp_query;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

    $pagination = array(
        'base' => @add_query_arg('page','%#%'),
        'format' => '',
        'total' => $wp_query->max_num_pages,
        'current' => $current,
        'prev_text' => __('ยซ Previous'),
        'next_text' => __('Next ยป'),
        'end_size' => 1,
        'mid_size' => 2,
        'show_all' => true,
        'type' => 'list'
    );

    if ( $wp_rewrite->using_permalinks() )
            $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

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

    echo paginate_links( $pagination );
}

In your template only my_paginate_links() will be called. And this is how the output looks like, of course you can adjust it to your liking:

WordPress Pagination

Comments are closed.

11 comments

  1. Paolo

    Hi, good article but it seems not working. The pagination is showen on my test blog but it doesn't change the page
    the URL published is localhost/index.php?page=2 but it doesn't work
    any clue? thx

  2. Cilice

    You know, Paolo, posting links to localhost won't help much - especially without any added code.

  3. Paolo

    You're right Cilice but I used exactly the code below in my functions.php and index.php page

    I can't explain much more about the problem... how can I check if something else is wrong? The code posted in this article works for you without any other setting?

    thanks

  4. Paolo

    I found the problem...
    the line

    # 'base' => @add_query_arg('page','%#%'),

    should be

    # 'base' => @add_query_arg('paged','%#%'),

    this way all works fine

  5. Markus

    cheers Paolo ๐Ÿ˜‰
    iยดll try it now ๐Ÿ™‚

  6. Wok

    Any idea what the $arg options

    'end_size' => 1,
    'mid_size' => 2,

    Do?

  7. Markus

    reference ๐Ÿ˜‰

    http://codex.wordpress.org/Function_Reference/paginate_links

  8. Wok

    Thanks,

    Some how i missed that...
    I was expecting a nice list detailing each parameter...got in the habit of reading the codex that way. It was in the paragraph.

    ๐Ÿ˜›

  9. Paul Adam Davis

    Hey all,

    I've worked on this function a little I made it work with pretty URLs as well as 'ugly' URLs without needing to change the code! ๐Ÿ™‚

    http://pauladamdavis.com/blog/2010/12/wordpress-pagination-again-again/

  10. Frank

    @Paul: great, thanks for sharing

  11. miniMAC

    Nice work, but ther's one problem in the search...

    When i input text in the search field, for example "music rock", are generated pages with the articles and is generated as the first: mydomain .com/url?s=music+rock

    at this point if I click on the second page, is generated this link instead: mydomain .com/page/2/url?s=musicrock

    and give me an error, because the "+" is missing and the words are combined....

    Have anyone a nice solution for this?

    Sorry for my bad eng ๐Ÿ™‚