Skip to content

Instantly share code, notes, and snippets.

@kirandash
Created April 19, 2017 07:42
Show Gist options
  • Save kirandash/410d896887b1c89b77b70dddd1871a6a to your computer and use it in GitHub Desktop.
Save kirandash/410d896887b1c89b77b70dddd1871a6a to your computer and use it in GitHub Desktop.
WP_Query pagination
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$resultsArgs = array(
'post_type' => 'results',
'posts_per_page' => 6,
'paged' => $paged
);
$resultsQuery = new WP_Query($resultsArgs);
if ( $resultsQuery->have_posts() ) :
?>
<div class="row">
<header class="section-header">
<h1 class="text-uppercase"><?php the_title(); ?></h1>
</header>
<?php
/* Start the Loop */
while ( $resultsQuery->have_posts() ) : $resultsQuery->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
echo '<div class="col-sm-4">';
get_template_part( 'template-parts/content', 'page-results' );
echo '</div>';
endwhile;
?>
</div><!-- .row -->
<?php if ($resultsQuery->max_num_pages > 1) : // check if the max number of pages is greater than 1 ?>
<nav class="navigation posts-navigation" role="navigation">
<h2 class="screen-reader-text"><?php _e('Posts navigation', 'tzmotorsport'); ?></h2>
<div class="nav-links">
<?php if(get_next_posts_link() != ''): ?>
<div class="nav-previous">
<?php echo get_next_posts_link( 'Prev', $resultsQuery->max_num_pages ); // display older posts link ?>
</div>
<?php endif; ?>
<?php if(get_previous_posts_link() != ''): ?>
<div class="nav-next">
<?php echo get_previous_posts_link( 'Next' ); // display newer posts link ?>
</div>
<?php endif; ?>
</div>
</nav>
<?php endif;
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment