Last active
April 2, 2022 06:27
-
-
Save jonathanlaf/f393ea7db4a5bc737130 to your computer and use it in GitHub Desktop.
[wpexplorer's WordPress pagination script enhanced.] #wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great