Skip to content

Instantly share code, notes, and snippets.

@5ally
Last active November 11, 2019 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5ally/c76588e33f44d3281c34ea9039a94859 to your computer and use it in GitHub Desktop.
Save 5ally/c76588e33f44d3281c34ea9039a94859 to your computer and use it in GitHub Desktop.
Using a modified pagination function. See https://stackoverflow.com/q/58709835 for details.
<?php // You should place this file in your-theme/inc
/**
* Pagination layout.
*
* @package understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! function_exists( 'understrap_pagination' ) ) {
function understrap_pagination( $args = array(), $class = 'pagination' ) {
$args = wp_parse_args(
$args,
array(
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __( '&laquo;', 'understrap' ),
'next_text' => __( '&raquo;', 'understrap' ),
'screen_reader_text' => __( 'Posts navigation', 'understrap' ),
'type' => 'array',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $GLOBALS['wp_query']->max_num_pages,
)
);
// Nothing to paginate, so let's bail.
if ( ! $links = paginate_links( $args ) ) {
return;
}
?>
<nav aria-label="<?php echo $args['screen_reader_text']; ?>">
<ul class="pagination">
<?php
foreach ( $links as $key => $link ) {
?>
<li class="page-item <?php echo strpos( $link, 'current' ) ? 'active' : ''; ?>">
<?php echo str_replace( 'page-numbers', 'page-link', $link ); ?>
</li>
<?php
}
?>
</ul>
</nav>
<?php
}
}
?>
<?php
/* Template Name: Test */
?>
<!-- Add other markup as necessary.. -->
<?php get_header(); ?>
<div class="bg-light">
<div class="container space-top-3 space-bottom-2">
<div class="row">
<?php
$paged = max( get_query_var( 'paged' ), get_query_var( 'page' ), 1 );
$args = array(
'post_type' => 'post',
'orderby' => 'post_date',
'order' => 'desc',
'perm' => 'readable',
'show_post_views' => true,
'posts_per_page' => 9,
'paged' => $paged
);
$latestArticles = new WP_Query( $args );
if ( $latestArticles->have_posts() ) :
while ( $latestArticles->have_posts() ) : $latestArticles->the_post();
get_template_part( 'loop-templates/content-search', get_post_format() );
endwhile;
?>
</div>
<div class="row mt-3">
<div class="col-auto">
<?php
understrap_pagination( [
'current' => $paged,
'total' => $latestArticles->max_num_pages,
] );
?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<!-- do nothing -->
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment