Skip to content

Instantly share code, notes, and snippets.

@matheuscl
Last active March 29, 2017 20:18
Show Gist options
  • Save matheuscl/78878c18758c3f2dde1e to your computer and use it in GitHub Desktop.
Save matheuscl/78878c18758c3f2dde1e to your computer and use it in GitHub Desktop.
WordPress custom pagination
<?php
/** Custom Pagination */
function custom_pagination($total_pages, $current_page, $pageByGet = true){
// Only paginate if we have more than one page
if ( $total_pages > 1 ) {
$big = 999999999;
// Structure of "format" depends on whether we’re using pretty permalinks
$permalink_structure = '';
$format = empty( $permalink_structure ) ? '&page=%#%' : 'page/%#%/';
$pages = paginate_links(array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => $format,
'current' => $current_page,
'total' => $total_pages,
'mid_size' => 2,
'type' => 'array',
'prev_next' => true,
'prev_text' => __('◄'),
'next_text' => __('►'),
));
$friendlyURL = ( $pageByGet === TRUE ? "?page=" : "page/" );
$pageLink = "";
foreach( $pages as $page){
$pageNum = strip_tags( $page );
$pagePrev = (int)$current_page - 1;
$pageNext = (int)$current_page + 1;
switch( $pageNum ){
case "&hellip;":
$pageLink .= "<li><a href='#'>" . $pageNum . "</a></li>";
break;
case "►":
$pageLink .= "<li><a href='" . $friendlyURL . $pageNext . "'>" . $pageNum . "</a></li>";
break;
case "◄":
$pageLink .= "<li><a href='" . $friendlyURL . $pagePrev . "'>" . $pageNum . "</a></li>";
break;
default:
$pageLink .= "<li><a href='" . $friendlyURL . $pageNum . "' class='" . ( $pageNum === $current_page ? "active" : "" ) . "' >" . $pageNum . "</a></li>";
break;
}
}
return $pageLink;
}
}
/** END Custom Pagination */
/** For this pagination works in index.php or an static page put this code before args of your query */
if( is_front_page() ){
$paged = (get_query_var( 'page' )) ? get_query_var( 'page' ) : 1;
}else{
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
}
/** Remember to put "paged" in args of your query */
//'paged' => $paged,
/** to show pagination only call the function with her args */
custom_pagination($query->max_num_pages, $paged);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment