So this is my current pagination Currently i am working with total of 6 pages so it shows 1 2 3 .. 5 6 (in the image below) 5 and 6 being the second last and last But when i reach the number 5 it still shows number 5 and 6 and again when i am at page number 4 and i can see page number 5 still shows 5 and 6 as per my approach (in the image below)
But i managed to hide the 5 and 6 when i am in the last page like this below
But i dont think this is a good approach as per mine. My requirement is that i want a list of pagination and it should display the last and second last and when i am at the last pages it should show first and second. How can i do that? Here's my current code
function pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged))
$paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
$pages = 1;
}
if(1 != $pages)
{
echo "<div class='paginations'>";
echo "<nav class='page-navigation'>";
echo "<ul class='pagination'>";
$first_page = $paged -1;
$last_page = $pages - $paged;
$second_last = $pages - 1;
if($first_page)
echo "<li class='page-item'><a aria-current='page' class='page-numbers page-link prev' href='".get_pagenum_link($paged - 1)."'>PREVIOUS</a></li>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li class='page-item'><a class='page-numbers page-link current'>".$i."</a></li>" : "<li class='page-item'><a class='page-numbers page-link' href='".get_pagenum_link($i)."'>".$i."</a></li>";
}
}
if(!($last_page == 0)){
if($pages > 5){
echo"<li class='page-item'><a class='page-numbers page-link'>..</a></li>";
echo"<li class='page-item'><a class='page-numbers page-link' href='".get_pagenum_link($second_last)."'>$second_last</a></li>";
echo"<li class='page-item'><a class='page-numbers page-link' href='".get_pagenum_link($pages)."'>$pages</a></li>";
}
}
if(!($last_page == 0)){
echo "<li class='page-item'><a class='next page-numbers page-link' href='".get_pagenum_link($paged + 1)."'>NEXT</a></li>";
}
echo "</ul></nav></div>";
}
}