Skip to content

Instantly share code, notes, and snippets.

@mhmoudsami
Last active July 24, 2016 15:47
Show Gist options
  • Select an option

  • Save mhmoudsami/2c3ce6c536b928dcb1acdc6183ca1290 to your computer and use it in GitHub Desktop.

Select an option

Save mhmoudsami/2c3ce6c536b928dcb1acdc6183ca1290 to your computer and use it in GitHub Desktop.
wordpress paginate_link that supports custom wp_query
if (! function_exists('prefix_Pagination')) :
/**
* Pagination.
*
* @link https://codex.wordpress.org/Function_Reference/paginate_links
* @param array $options paginate_links options
* @param wp_query $query wp_query
* @return mixed false if no paging or pagination html string
*/
function prefix_Pagination($options=[], $wp_query = null)
{
// if no query passed use the default wordpress query
$wp_query = ( ! $wp_query ) ? $GLOBALS['wp_query'] : $wp_query;
$big = 999999999;
// static home page uses "page" query var while all other pages uses "paged" query var
$paged_text = ( is_front_page() && 'posts' !== get_option( 'show_on_front' ) ) ? "page" : "paged";
$defaults = [
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var($paged_text) ),
'total' => $wp_query->max_num_pages,
'type' => 'list',
'prev_text' => __('«'),
'next_text' => __('»'),
];
$options = wp_parse_args($options, $defaults);
return paginate_links($options);
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment