Skip to content

Instantly share code, notes, and snippets.

@saidMounaim
Created October 13, 2021 16:08
Show Gist options
  • Save saidMounaim/b653ef2478ce36f5cab09ca5e34c2e10 to your computer and use it in GitHub Desktop.
Save saidMounaim/b653ef2478ce36f5cab09ca5e34c2e10 to your computer and use it in GitHub Desktop.
Custom pagination in wordpress
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged,
)
); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<!-- do stuff -->
<?php endwhile; wp_reset_query(); ?>
<?php
// TOTAL OF POSTS
$totalpost = $loop->found_posts;
// TOTAL OF PAGES
$totalPages = ceil($totalpost / 6);
$pages = [];
for($i = 1; $i <= $totalPages; $i++ ) {
array_push($pages, $i);
}
?>
<div class="paginate">
<div class="left-paginate">
<span>Page <?php echo $paged; ?> of <?php echo $totalPages; ?></span>
</div>
<div class="right-paginate">
<ul>
<li class="first" >
<a href="<?php the_permalink() ?>page/<?php if($paged !== 1): echo $paged - 1; endif; ?>">
<<
</a>
</li>
<li class="prev" >
<a href="<?php the_permalink() ?>page/<?php echo 1; ?>">
<
</a>
</li>
<ul class="numbers">
<?php foreach($pages as $page): ?>
<li>
<a href="<?php the_permalink() ?>page/<?php echo $page ?>" class="<?php if($page == $paged): echo "active"; endif; ?>" >
0<?php echo $page; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<li class="next" >
<a href="<?php the_permalink() ?>page/<?php if($paged !== $totalPages): echo $paged + 1; endif; ?>">
>
</a>
</li>
<li class="last" >
<a href="<?php the_permalink() ?>page/<?php echo $totalPages; ?>">
>>
</a>
</li>
</ul>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment