Created
March 7, 2022 09:52
-
-
Save encoderit-arman/aeefa08d032a84212d0e48b011243266 to your computer and use it in GitHub Desktop.
php pagination , wordpress pagination
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
<?php | |
$total_pages = $product_data->max_num_pages; | |
$current_page = (isset($_GET['page'])) ? $_GET['page'] : 1; | |
if ($current_page == 1) { | |
$prev_page = false; | |
} else { | |
$prev_page = $current_page - 1; | |
} | |
if ($current_page == $total_pages) { | |
$next_page = false; | |
} else { | |
$next_page = $current_page + 1; | |
} | |
$page_prev_array = []; | |
$page_next_array = []; | |
for ($i = 1; $i <= $total_pages; $i++) { | |
if ($current_page == $i) { | |
array_push($page_prev_array, $i); | |
} elseif ((0 <= ($current_page - $i)) && (($current_page - $i) < 3)) { | |
array_push($page_prev_array, $i); | |
} elseif ((0 < ($i - $current_page)) && (($i - $current_page) < 4)) { | |
array_push($page_next_array, $i); | |
} | |
} | |
if ($total_pages > 8) { | |
array_push($page_prev_array, '...'); | |
} | |
$final_array = array_merge($page_prev_array, $page_next_array); | |
// var_dump($final_array); | |
?> | |
<style> | |
.page-item.disabled { | |
pointer-events: none; | |
cursor: default; | |
} | |
</style> | |
<nav aria-label="Page navigation "> | |
<ul class="pagination"> | |
<?php if ($total_pages != 1) { ?> | |
<?php if ($prev_page != false) { ?> | |
<li class="page-item"><a class="page-link bg-primary" href="?page=<?php echo $prev_page; ?>">Previous</a></li> | |
<?php } ?> | |
<?php | |
foreach ($final_array as $paginate) { ?> | |
<li class="page-item"><a class="page-link <?php echo ($current_page == $paginate) ? 'current-page-paginate-active' : ''; ?>" href="<?php echo ($paginate != '...') ? '?page=' . $paginate : ''; ?>"><?php echo $paginate; ?></a></li> | |
<?php } ?> | |
<?php if ($next_page != false) { ?> | |
<li class="page-item"><a class="page-link bg-primary" href="?page=<?php echo $next_page; ?>">Next</a></li> | |
<?php } ?> | |
<?php } ?> | |
</ul> | |
</nav> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment