Skip to content

Instantly share code, notes, and snippets.

@flegfleg
Last active September 3, 2020 21:38
Show Gist options
  • Save flegfleg/e03a2680c4392efbf9f06a7fa3f073f0 to your computer and use it in GitHub Desktop.
Save flegfleg/e03a2680c4392efbf9f06a7fa3f073f0 to your computer and use it in GitHub Desktop.
Front page with sub query and pagination
<?php
/**
* Template Name: Front page
*
* Template Post Type: page
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package _s
*/
get_header(); ?>
<main id="main" class="site-main ">
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) :
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.
*/
get_template_part( 'template-parts/content', 'page' );
endwhile;
?>
<h2 class="section-title">News</h2>
<div class="grid-layout">
<?php
global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;
$news_query_args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'category' => 1,
'paged' => $paged
);
$news_query = new WP_Query( $news_query_args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $news_query;
$pagination = array(
'base' => '%_%',
'format' => '?page=%#%',
'total' => $news_query->max_num_pages,
'prev_next' => True,
'prev_text' => __( '<< Previous' ),
'next_text' => __( 'Next >>' )
);
if ( $news_query->have_posts() ) :
while ( $news_query->have_posts() ) : $news_query->the_post();
get_template_part( 'template-parts/content', 'grid-posts' );
endwhile;
endif;
?>
</div><!-- grid-layout -->
<?php _s_display_numeric_pagination();
// wp_reset_postdata();
$wp_query = NULL;
$wp_query = $temp_query;
?>
<div class="more container">
<a class="button"><?php echo __('More current things', '_s'); ?></a>
</div>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment