Skip to content

Instantly share code, notes, and snippets.

@khleomix
Last active February 16, 2022 20:36
Show Gist options
  • Save khleomix/df7c3c2f0bbace2ea52d034e149c0af7 to your computer and use it in GitHub Desktop.
Save khleomix/df7c3c2f0bbace2ea52d034e149c0af7 to your computer and use it in GitHub Desktop.
Pagination with Offset
<?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' => '&laquo;',
'next_text' => '&raquo;',
)
);
echo '</div><!-- .pagination-container -->';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment