Skip to content

Instantly share code, notes, and snippets.

@techb
Created April 26, 2022 16:01
Show Gist options
  • Save techb/e8bebe88a0b4cae2240a8203ddea57f0 to your computer and use it in GitHub Desktop.
Save techb/e8bebe88a0b4cae2240a8203ddea57f0 to your computer and use it in GitHub Desktop.
WordPress Frontpage blog list pagination. How to use pagination with WP_Query.
<?php
/**
* Template Name: Homepage
*/
get_header();
// since this is the Front-Page we need to use "page" instead of "paged"
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$args = array(
"posts_per_page" => 6,
"post_type" => "post",
"post_status" => "publish",
"paged" => $paged
);
$post_query = new WP_Query($args);
?>
<main id="primary" class="site-main">
<section class="home-page">
<div class="hero">
<!-- add page hero -->
</div>
<div class="page-content">
<?php while( $post_query->have_posts() ){
$post_query->the_post();
get_template_part( 'template-parts/content', get_post_type() );
} ?>
<div class="pagination-container">
<?php
// Yoink: https://wordpress.stackexchange.com/a/254200
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $post_query->max_num_pages,
'current' => max( 1, get_query_var( 'page' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
</div>
</section>
</main>
<?php
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment