| 1 |
<?php |
| 2 |
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- view template receives variables via extract(); prefixing is impractical. |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
if ( $total_pages <= 1 ) { |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
if ( $template != 'doc_category' ) { |
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
// Number of page links to show before and after current page |
| 16 |
$links_to_show = 2; |
| 17 |
// Current page, fallback to 1 if not set |
| 18 |
$current_page = $current_page ?: 1; |
| 19 |
?> |
| 20 |
|
| 21 |
<div class="betterdocs-pagination"> |
| 22 |
<ul> |
| 23 |
<?php if ( $current_page > 1 ) : ?> |
| 24 |
<li class="prev"> |
| 25 |
<a href="<?php echo esc_url( $link ) . 'page/' . ( intval( $current_page ) - 1 ); ?>">❮</a> |
| 26 |
</li> |
| 27 |
<?php endif; ?> |
| 28 |
|
| 29 |
<?php |
| 30 |
// Always show first page |
| 31 |
$pages_to_show = [ 1 ]; |
| 32 |
|
| 33 |
// Calculate start and end of the middle range |
| 34 |
$start = max( 2, $current_page - $links_to_show ); |
| 35 |
$end = min( $total_pages - 1, $current_page + $links_to_show ); |
| 36 |
|
| 37 |
// Add ellipsis after first page if needed |
| 38 |
if ( $start > 2 ) { |
| 39 |
$pages_to_show[] = '...'; |
| 40 |
} |
| 41 |
|
| 42 |
// Add middle range pages |
| 43 |
for ( $i = $start; $i <= $end; $i++ ) { |
| 44 |
$pages_to_show[] = $i; |
| 45 |
} |
| 46 |
|
| 47 |
// Add ellipsis before last page if needed |
| 48 |
if ( $end < $total_pages - 1 ) { |
| 49 |
$pages_to_show[] = '...'; |
| 50 |
} |
| 51 |
|
| 52 |
// Always show last page if total pages > 1 |
| 53 |
if ( $total_pages > 1 ) { |
| 54 |
$pages_to_show[] = $total_pages; |
| 55 |
} |
| 56 |
|
| 57 |
// Output the page links |
| 58 |
foreach ( $pages_to_show as $i ) : |
| 59 |
if ( $i === '...' ) : |
| 60 |
?> |
| 61 |
<li class="ellipsis"><span><?php echo esc_html( $i ); ?></span></li> |
| 62 |
<?php |
| 63 |
else : |
| 64 |
?> |
| 65 |
<li class="<?php echo $current_page == $i ? 'active' : ''; ?>"> |
| 66 |
<a href="<?php echo esc_url( $link ) . 'page/' . esc_html( $i ); ?>"><?php echo esc_html( $i ); ?></a> |
| 67 |
</li> |
| 68 |
<?php |
| 69 |
endif; |
| 70 |
endforeach; |
| 71 |
?> |
| 72 |
|
| 73 |
<?php if ( $current_page < $total_pages ) : ?> |
| 74 |
<li class="next"> |
| 75 |
<a href="<?php echo esc_url( $link ) . 'page/' . ( intval( $current_page ) + 1 ); ?>">❯</a> |
| 76 |
</li> |
| 77 |
<?php endif; ?> |
| 78 |
</ul> |
| 79 |
</div> |
| 80 |
|