Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active August 26, 2023 07:52
Show Gist options
  • Save james2doyle/414bbfe37a51f3907e90 to your computer and use it in GitHub Desktop.
Save james2doyle/414bbfe37a51f3907e90 to your computer and use it in GitHub Desktop.
Simple pagination element with dot separation when large counts
<div class="pagination size1of1 unit">
<?php if($paged == 1): ?>
<span class="current"><?php echo $paged ?></span>
<?php else: ?>
<a class="prev" href="?page=<?php echo ($current_page - 1) ?>">Prev</a>
<?php if ($current_page > 2): ?>
<a href="?page=1">1</a>
<?php endif ?>
<?php if ($current_page > 3): ?>
<span class="dots">...</span>
<?php endif ?>
<?php endif ?>
<?php if($current_page - 1 > 0): ?>
<a href="?page=<?php echo $current_page - 1 ?>"><?php echo $current_page - 1 ?></a>
<?php endif ?>
<span class="current"><?php echo $current_page ?></span>
<?php if ($current_page + 1 < $max_num_pages): ?>
<a href="?page=<?php echo $current_page + 1 ?>"><?php echo $current_page + 1 ?></a>
<?php endif ?>
<?php if($current_page < $max_num_pages): ?>
<?php if ($current_page < $max_num_pages - 2): ?>
<span class="dots">...</span>
<?php endif ?>
<a href="?page=<?php echo $max_num_pages ?>"><?php echo $max_num_pages ?></a>
<a class="next" href="?page=<?php echo ($current_page + 1) ?>">Next</a>
<?php endif ?>
</div>
@pimpattcha
Copy link

Do you mind if I ask you a question? I'm curious what is $paged variable on the second line for? the code works out great without that variable and that 'if' case. Thank you so much btw, this function helps me out a lot.

@udosenakane
Copy link

As @pimpattcha pointed out, it works well without the first if statement.
Thanks and you save the day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment