Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[wpexplorer's WordPress pagination script enhanced.] #wordpress
/*
* Numbered Pagination
* http://www.wpexplorer.com/pagination-wordpress-theme/
*/
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination($wpex_query) {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
/*
* Using ${varname} check for a variable with the same name has the value of varname
* http://www.php.net/manual/en/language.variables.variable.php
* P.S. I know $$varname procude the same result and is much short, but I personnaly prefer the ${}
*/
global $wp_query,${$wpex_query};
if ( ${$wpex_query} ) {
$total = ${$wpex_query}->max_num_pages;
} else {
$total = $wp_query->max_num_pages;
}
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
/* And here is an exemple of usage in a template */
$activities = array();
$activities_args = array( 'post_type' => 'activities' );
$activities_query = new WP_Query( $activities_args );
if ( $activities_query->have_posts() ) {
while ( $activities_query->have_posts() ) {
$activities_query->the_post();
the_title( '<h1>', '</h1>' );
the_content();
} /* while ( have_posts() ) */
} /* if ( have_posts() ) */
wpex_pagination('activities_query');
@engrmostafijur

This comment has been minimized.

Copy link

engrmostafijur commented Mar 3, 2015

great

@roeeyossef

This comment has been minimized.

Copy link

roeeyossef commented Nov 5, 2017

nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.