Last active
July 30, 2021 10:16
-
-
Save vikaskhunteta/874d664651a1fd82f4c16b08e6b4e3dd to your computer and use it in GitHub Desktop.
Pagination for different custom post type inside single pages using WP Page Navi Plugin
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 | |
$paged = 1; | |
if ( isset( $_GET['page_no'] ) ) { | |
$paged = (int)$_GET['page_no']; | |
} else { | |
$paged = 1; | |
} | |
if ( ! $paged ) { | |
$paged = 1; | |
} | |
$args = array( | |
'post_type' => 'your_post_type', | |
'post_status' => 'publish', | |
'posts_per_page' => '10', | |
'paged' => $paged, | |
); | |
$the_query = new WP_Query( $args ); | |
<?php if ( $the_query->max_num_pages > 1 ) { ?> | |
<div class='pagination'> | |
<?php | |
if ( function_exists( 'wp_pagenavi_modified' ) ) { | |
// default echo | |
wp_pagenavi_modified( array( 'query' => $the_query ) ); | |
} | |
?> | |
</div> | |
<?php } //endif ?> | |
<?php | |
// Extend PavNavi_Call Class To Fix Pagination on Single Pages | |
// Check after all plugins loaded | |
add_action( 'plugins_loaded', function() { | |
if ( class_exists( 'PageNavi_Call' ) ) { | |
class PageNavi_Call_Extended extends PageNavi_Call | |
{ | |
function get_url( $page ) { | |
$url = parse_url( $_SERVER['REQUEST_URI'] ); | |
$href = '?page_no='.$page; | |
if ( isset( $url['query'] ) ) { | |
if ( isset( $_GET['page_no'] ) ) { | |
$url['query'] = str_replace( "page_no=".$_GET['page_no'], "", $url['query'] ); | |
} | |
if ( $url['query'] ) { | |
$href = '?page_no='.$page . '&' . $url['query']; | |
} | |
} | |
$href = str_replace( "&&", "&", $href ); | |
return $href; | |
} | |
} | |
// This is exact copy of original wp_pagenavi function except it calls extended PageNavi_Call_Extended class and only normal styling is supported | |
function wp_pagenavi_modified( $args = array() ) { | |
if ( !is_array( $args ) ) { | |
$argv = func_get_args(); | |
$args = array(); | |
foreach ( array( 'before', 'after', 'options' ) as $i => $key ) { | |
$args[ $key ] = isset( $argv[ $i ]) ? $argv[ $i ] : ''; | |
} | |
} | |
$args = wp_parse_args( $args, array( | |
'before' => '', | |
'after' => '', | |
'wrapper_tag' => 'div', | |
'wrapper_class' => 'wp-pagenavi', | |
'options' => array(), | |
'query' => $GLOBALS['wp_query'], | |
'type' => 'posts', | |
'echo' => true | |
) ); | |
extract( $args, EXTR_SKIP ); | |
$options = wp_parse_args( $options, PageNavi_Core::$options->get() ); | |
$instance = new PageNavi_Call_Extended( $args ); | |
list( $posts_per_page, $paged, $total_pages ) = $instance->get_pagination_args(); | |
if ( 1 == $total_pages && !$options['always_show'] ) | |
return; | |
$pages_to_show = absint( $options['num_pages'] ); | |
$larger_page_to_show = absint( $options['num_larger_page_numbers'] ); | |
$larger_page_multiple = absint( $options['larger_page_numbers_multiple'] ); | |
$pages_to_show_minus_1 = $pages_to_show - 1; | |
$half_page_start = floor( $pages_to_show_minus_1/2 ); | |
$half_page_end = ceil( $pages_to_show_minus_1/2 ); | |
$start_page = $paged - $half_page_start; | |
if ( $start_page <= 0 ) | |
$start_page = 1; | |
$end_page = $paged + $half_page_end; | |
if ( ( $end_page - $start_page ) != $pages_to_show_minus_1 ) | |
$end_page = $start_page + $pages_to_show_minus_1; | |
if ( $end_page > $total_pages ) { | |
$start_page = $total_pages - $pages_to_show_minus_1; | |
$end_page = $total_pages; | |
} | |
if ( $start_page < 1 ) | |
$start_page = 1; | |
// Support for filters to change class names | |
$class_names = array( | |
'pages' => apply_filters( 'wp_pagenavi_class_pages', 'pages'), | |
'first' => apply_filters( 'wp_pagenavi_class_first', 'first' ), | |
'previouspostslink' => apply_filters( 'wp_pagenavi_class_previouspostslink', 'previouspostslink' ), | |
'extend' => apply_filters( 'wp_pagenavi_class_extend', 'extend' ), | |
'smaller' => apply_filters( 'wp_pagenavi_class_smaller', 'smaller' ), | |
'page' => apply_filters( 'wp_pagenavi_class_page', 'page' ), | |
'current' => apply_filters( 'wp_pagenavi_class_current', 'current'), | |
'larger' => apply_filters( 'wp_pagenavi_class_larger', 'larger' ), | |
'nextpostslink' => apply_filters( 'wp_pagenavi_class_nextpostslink', 'nextpostslink'), | |
'last' => apply_filters( 'wp_pagenavi_class_last', 'last'), | |
); | |
$out = ''; | |
switch ( intval( $options['style'] ) ) { | |
// Normal | |
case 1: | |
// Text | |
if ( !empty( $options['pages_text'] ) ) { | |
$pages_text = str_replace( | |
array( "%CURRENT_PAGE%", "%TOTAL_PAGES%" ), | |
array( number_format_i18n( $paged ), number_format_i18n( $total_pages ) ), | |
__( $options['pages_text'], 'wp-pagenavi' ) ); | |
$out .= "<span class='{$class_names['pages']}'>$pages_text</span>"; | |
} | |
if ( $start_page >= 2 && $pages_to_show < $total_pages ) { | |
// First | |
$first_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $total_pages ), __( $options['first_text'], 'wp-pagenavi' ) ); | |
$out .= $instance->get_single( 1, $first_text, array( | |
'class' => $class_names['first'], | |
'aria-label' => __('First Page'), | |
), '%TOTAL_PAGES%' ); | |
} | |
// Previous | |
if ( $paged > 1 && !empty( $options['prev_text'] ) ) { | |
$out .= $instance->get_single( $paged - 1, $options['prev_text'], array( | |
'class' => $class_names['previouspostslink'], | |
'rel' => 'prev', | |
'aria-label' => __('Previous Page'), | |
) ); | |
} | |
if ( $start_page >= 2 && $pages_to_show < $total_pages ) { | |
if ( !empty( $options['dotleft_text'] ) ) | |
$out .= "<span class='{$class_names['extend']}'>{$options['dotleft_text']}</span>"; | |
} | |
// Smaller pages | |
$larger_pages_array = array(); | |
if ( $larger_page_multiple ) | |
for ( $i = $larger_page_multiple; $i <= $total_pages; $i+= $larger_page_multiple ) | |
$larger_pages_array[] = $i; | |
$larger_page_start = 0; | |
foreach ( $larger_pages_array as $larger_page ) { | |
if ( $larger_page < ($start_page - $half_page_start) && $larger_page_start < $larger_page_to_show ) { | |
$out .= $instance->get_single( $larger_page, $options['page_text'], array( | |
'class' => "{$class_names['smaller']} {$class_names['page']}", | |
'title' => sprintf( __( 'Page %s', 'wp-pagenavi' ), number_format_i18n( $larger_page ) ), | |
) ); | |
$larger_page_start++; | |
} | |
} | |
if ( $larger_page_start ) | |
$out .= "<span class='{$class_names['extend']}'>{$options['dotleft_text']}</span>"; | |
// Page numbers | |
$timeline = 'smaller'; | |
foreach ( range( $start_page, $end_page ) as $i ) { | |
if ( $i == $paged && !empty( $options['current_text'] ) ) { | |
$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] ); | |
$out .= "<span aria-current='page' class='{$class_names['current']}'>$current_page_text</span>"; | |
$timeline = 'larger'; | |
} else { | |
$out .= $instance->get_single( $i, $options['page_text'], array( | |
'class' => "{$class_names['page']} {$class_names[$timeline]}", | |
'title' => sprintf( __( 'Page %s', 'wp-pagenavi' ), number_format_i18n( $i ) ), | |
) ); | |
} | |
} | |
// Large pages | |
$larger_page_end = 0; | |
$larger_page_out = ''; | |
foreach ( $larger_pages_array as $larger_page ) { | |
if ( $larger_page > ($end_page + $half_page_end) && $larger_page_end < $larger_page_to_show ) { | |
$larger_page_out .= $instance->get_single( $larger_page, $options['page_text'], array( | |
'class' => "{$class_names['larger']} {$class_names['page']}", | |
'title' => sprintf( __( 'Page %s', 'wp-pagenavi' ), number_format_i18n( $larger_page ) ), | |
) ); | |
$larger_page_end++; | |
} | |
} | |
if ( $larger_page_out ) { | |
$out .= "<span class='{$class_names['extend']}'>{$options['dotright_text']}</span>"; | |
} | |
$out .= $larger_page_out; | |
if ( $end_page < $total_pages ) { | |
if ( !empty( $options['dotright_text'] ) ) | |
$out .= "<span class='{$class_names['extend']}'>{$options['dotright_text']}</span>"; | |
} | |
// Next | |
if ( $paged < $total_pages && !empty( $options['next_text'] ) ) { | |
$out .= $instance->get_single( $paged + 1, $options['next_text'], array( | |
'class' => $class_names['nextpostslink'], | |
'rel' => 'next', | |
'aria-label' => __('Next Page'), | |
) ); | |
} | |
if ( $end_page < $total_pages ) { | |
// Last | |
$out .= $instance->get_single( $total_pages, __( $options['last_text'], 'wp-pagenavi' ), array( | |
'class' => $class_names['last'], | |
'aria-label' => __('Last Page'), | |
), '%TOTAL_PAGES%' ); | |
} | |
break; | |
} | |
$out = $before . "<" . $wrapper_tag . " class='" . $wrapper_class . "' role='navigation'>\n$out\n</" . $wrapper_tag . ">" . $after; | |
$out = apply_filters( 'wp_pagenavi_modified', $out, $args ); | |
if ( !$echo ) | |
return $out; | |
echo $out; | |
} | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment