WordPress Custom Pagination

 

There are a more than a few ways to setup WordPress pagination, or “paged” links, in a WordPress theme. One popular method involves using the WP-PageNavi plugin. This plugin is great for someone non-technical who wants to add pagination onto their current WordPress site, however it’s not an ideal option for a theme developer.

WordPress function to handle the heavy lifting. The paginate_links() function makes it’s incredibly simple to add pagination to your WordPress themes. Here’s how I typically use it.

Put this code after end of while loop.

 

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type' => 'project',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged
);
query_posts($args);
if ( have_posts() ) : while (have_posts()) : the_post();

/*Your content*/

endwhile; endif;

//pagination code

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!

if ( $total > 1 )
{
// get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// structure of "format" depends on whether we're using pretty permalinks
if( get_option('permalink_structure') )
{
$format = 'page/%#%/';
}
else
{
$format = 'page/%#%/';
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'prev_text' => __('Prev'),
'next_text' => __('Next'),
'type' => 'list'
));
}
wp_reset_query();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s