So the paginated links not working for you and you now want to know how you can create a custom pagination. Have no fear the code below is all you need. Its well commented and self explanatory.
function my_custom_pagination( $total = '', $show_next = 4 ) {
// return nothing if is single pag
if ( is_singular() ) {
return;
}
global $wp_query;
/** Stop execution if there's only 1 page */
if ( empty( $total ) ) {
if ( $wp_query->max_num_pages <= 1 ) { return; } } // get current paged value $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $max = empty( $total ) ? intval( $wp_query->max_num_pages ) : intval( $total );
/** Add current page to the array */
/*The switch cases below is so show the number of pagination before and after the current
* Example: If the current page is 7 show total numbers you want to show before and after 7 is 2.
* So this means before 7 you will see 5 and 6 and after 7 you will see 8 and 9 You can fiddle around the
* switch case until you get your deired result
*/
switch ( $paged ):
case 1:
$links[] = $paged;
if ( $max < 4 ) { $show_next = $max; } break; case 2: if ( $paged != $max && $max > 4 ) {
$links[] = $paged;
$show_next = 3;
} else if ( $paged == $max ) {
$links[] = $paged;
$show_next = 1;
} else {
$links[] = $paged;
$show_next = 2;
}
break;
case 3:
if ( $paged != $max ) {
$links[] = $paged - 1;
$links[] = $paged;
$show_next = 2;
} else if ( $paged == $max ) {
$links[] = $paged - 1;
$links[] = $paged;
$show_next = 1;
}
break;
default:
/*To works if paged and maixum post are not equal*/
if ( $paged != $max ) {
$links[] = $paged - 1;
$links[] = $paged;
$show_next = 2;
} else if ( $paged == $max ) {
$links[] = $paged - 2;
$links[] = $paged - 1;
$links[] = $paged;
$show_next = 1;
}
endswitch;
$next_page = $paged + 1;
$show_pagination = $paged + $show_next - 1;
for ( $i = $next_page; $i <= $show_pagination; $i ++ ) {
$links[] = $i;
}
/** Add the pages around the current page to the array */
echo '
<div class="pagination-holder">' . "\n";
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' "current "' : '';
printf( '<a class="page-numbers %s" href="%s">%s</a></li>
' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' current' : '';
if ( $paged == $link ) {
printf( '<span class="page-numbers %s" >%s</span>' . "\n", $class, $link );
} else {
printf( '<a class="page-numbers %s" href="%s">%s</a>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
$class = $paged == $max ? ' active' : '';
printf( '<span class="page-numbers dots">…</span><a class="page-numbers %s" href="%s">%s</a>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() ) {
printf( '%s' . "\n", get_next_posts_link( 'Next &nbsp; <i class="fa fa-angle-right"></i>' ) );
} else if ( get_previous_posts_link() ) {
printf( '%s' . "\n", get_previous_posts_link( '<i class="fa fa-angle-left"></i> &nbsp;Previous ' ) );
}
echo '</div>
' . "\n";
}
I hope it helped you out. If you have any confusion or any queries, post them in the comments below and I will get back to you. Happy Coding