Skip to content

Instantly share code, notes, and snippets.

@sunilw
Created January 8, 2023 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunilw/55168d243a7d275b50a0959e4bb9a8d0 to your computer and use it in GitHub Desktop.
Save sunilw/55168d243a7d275b50a0959e4bb9a8d0 to your computer and use it in GitHub Desktop.
<?php
/**
* A loop a gives post previews and pagination
*/
function sw_paginated_blog_loop()
{
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => '6',
'paged' => $paged
);
$query = new WP_Query($args); ?>
<?php if ($query->have_posts()) { ?>
<div class="superhero-container">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="superhero">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<?php if ($query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Articles', $query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Articles' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
<?php } //endif
?>
<?php } // ends sw_paginated_blog_loop ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment