X

Add pagination to your blog

Add the first block of code to the functions.php of your wordpress theme, then just add the my_paginate_links() function in the location you wish to have your pagination appear.



Code Snippet:


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('paged','%#%'),
'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 );
}


Code Snippet:


<?
my_paginate_links();
?>

Comments  Leave a Reply

  1. This code work with “WP_Query();” ? please explain how

    1. WP_Query() has a pagination parameter ‘paged’ all put into paginate_links
      http://codex.wordpress.org/Function_Reference/paginate_links

      The WP_Query codex page has more details then I can explain within the comments. Including code samples etc.
      http://codex.wordpress.org/Class_Reference/WP_Query

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!