Last active
February 16, 2022 20:36
-
-
Save khleomix/df7c3c2f0bbace2ea52d034e149c0af7 to your computer and use it in GitHub Desktop.
Pagination with Offset
This file contains 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 | |
/** | |
* Displays numeric pagination with offset. | |
* | |
* @param array $custom_query Custom WP_Query. | |
* @return void | |
* @author JC Palmes | |
*/ | |
function display_numeric_pagination_with_offset( $custom_query ) { | |
$big = 999999999; | |
$current_page = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; | |
$current_page = max( 1, $current_page ); | |
$posts_per_page = 9; | |
$offset_start = 1; | |
$offset = ( $current_page - 1 ) * $posts_per_page + $offset_start; | |
$total_rows = max( 0, $custom_query->found_posts - $offset_start ); | |
$total_pages = ceil( $total_rows / $posts_per_page ); | |
echo '<div class="pagination-container">'; | |
echo paginate_links( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
array( | |
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ), | |
'format' => '?paged=%#%', | |
'current' => $current_page, | |
'total' => $total_pages, | |
'prev_text' => '«', | |
'next_text' => '»', | |
) | |
); | |
echo '</div><!-- .pagination-container -->'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment