Last active
July 8, 2021 07:58
-
-
Save cfxd/65f82bf88fab44a37f4a1cd844ba96cf to your computer and use it in GitHub Desktop.
Paginate Links with URL params. See https://cfxdesign.com/wordpress-custom-query-pagination/
This file contains hidden or 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
<?php | |
global $wp_query; | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
$prev_text = __('Newer Posts'); | |
$next_text = __('Older Posts'); | |
// The magic happens here. First we grab the URL and check it for URL params, | |
// if they're present then we remove them, insert the $format placeholder, | |
// and append them to the end of the URL. | |
$url_params_regex = '/\?.*?$/'; | |
preg_match($url_params_regex, get_pagenum_link(), $url_params); | |
$base = !empty($url_params[0]) ? preg_replace($url_params_regex, '', get_pagenum_link()).'%_%/'.$url_params[0] : get_pagenum_link().'%_%'; | |
$format = 'page/%#%'; | |
$blog_nav = paginate_links(array( | |
'base' => $base, | |
'format' => $format, | |
'current' => max(1, get_query_var('paged')), | |
'total' => $wp_query->max_num_pages, | |
'prev_next' => true, | |
'prev_text' => $prev_text, | |
'next_text' => $next_text, | |
'type' => 'array', | |
'end_size' => 3, | |
'mid_size' => 3 | |
)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment