Last active
October 20, 2022 10:36
-
-
Save opicron/bedd151d5ba1218819070dd839087559 to your computer and use it in GitHub Desktop.
facetwp custom pagination #php #facetwp
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
add_filter( 'facetwp_pager_html', function( $output, $params ) { | |
// show all pages | |
/* | |
$output = ''; | |
if ( 1 < $params['total_pages'] ) { | |
for ( $i = 1; $i <= $params['total_pages']; $i++ ) { | |
$is_curr = ( $i === $params['page'] ) ? ' active' : ''; | |
$output .= '<a class="facetwp-page' . $is_curr . '" data-page="' . $i . '">' . $i . '</a>'; | |
} | |
} | |
*/ | |
// next and previous page buttons | |
/* | |
$page = $params['page']; | |
$total_pages = $params['total_pages']; | |
if ( $page < $total_pages && $total_pages > 1 ) { | |
$output = '<a class="facetwp-page" data-page="' . ($page + 1) . '">Next</a>' . $output; | |
} | |
if ( $page > 1 ) { | |
$output = '<a class="facetwp-page" data-page="' . ($page - 1) . '">Previous</a>' . $output; | |
} | |
*/ | |
// match genesis pager style | |
$page = (int) $params['page']; | |
$total_pages = (int) $params['total_pages']; | |
// Only show pagination when > 1 page. | |
$output = '<nav class="woocommerce-pagination"><ul class="page-numbers">'; | |
if ( 1 < $total_pages ) { | |
if ( 1 < $page ) { | |
$output .= '<li><a class="facetwp-page page-numbers" data-page="' . ( $page - 1 ) . '"><</a></li> '; | |
} | |
if ( 3 < $page ) { | |
$output .= '<li><a class="facetwp-page page-numbers first-page" data-page="1"><span class="screen-reader-text">Page </span>1</a></li>'; | |
$output .= ' <li class="pagination-omission">…</li> '; | |
} | |
for ( $i = 2; $i > 0; $i-- ) { | |
if ( 0 < ( $page - $i ) ) { | |
$output .= '<li><a class="facetwp-page page-numbers" data-page="' . ($page - $i) . '"><span class="screen-reader-text">Page </span>' . ($page - $i) . '</a></li> '; | |
} | |
} | |
// Current page. | |
$output .= '<li class="active"><span class="page-numbers current" aria-label="Current page" data-page="' . $page . '"><span class="screen-reader-text">Page </span>' . $page . '</span></li>'; | |
for ( $i = 1; $i <= 2; $i++ ) { | |
if ( $total_pages >= ( $page + $i ) ) { | |
$output .= ' <li><a class="facetwp-page page-numbers" data-page="' . ($page + $i) . '"><span class="screen-reader-text">Page </span>' . ($page + $i) . '</a></li>'; | |
} | |
} | |
if ( $total_pages > ( $page + 2 ) ) { | |
$output .= ' <li class="pagination-omission">…</li> '; | |
$output .= '<li><a class="facetwp-page page-numbers last-page" data-page="' . $total_pages . '"><span class="screen-reader-text">Page </span>' . $total_pages . '</a></li>'; | |
} | |
if ( $page < $total_pages ) { | |
$output .= ' <li><a class="facetwp-page page-numbers" data-page="' . ( $page + 1 ) . '">></a></li>'; | |
} | |
} | |
$output .= '</ul></nav>'; | |
return $output; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment