Skip to content

Instantly share code, notes, and snippets.

@addisonhall
Last active October 22, 2015 16:11
Show Gist options
  • Save addisonhall/dd18640a7aefc5005502 to your computer and use it in GitHub Desktop.
Save addisonhall/dd18640a7aefc5005502 to your computer and use it in GitHub Desktop.
WP page template with custom query for announcments
<?php
/**
* Template part for displaying announcements.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package chapel
*/
?>
<article class="row append-btm">
<div class="col-sm-12">
<?php the_title( sprintf( '<h2 class="h3 entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
<?php the_excerpt(); ?>
</div>
</article>
<?php
/**
* Page template for displaying announcements.
*
* Template Name: Announcements
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package chapel
*/
get_header(); ?>
<?php get_template_part( 'partials/partial', 'pageheader' ); ?>
<div id="primary" class="content-area container">
<main id="main" class="site-main col-sm-8" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'page' ); ?>
<?php endwhile; // End of the loop. ?>
<?php
/*
* Get announcements with custom query
*/
$args = array (
'post_type' => 'announcements',
'posts_per_page' => 5,
'paged' => $paged // TODO: why do we need this?
);
$announcement_query = new WP_Query( $args );
?>
<?php if ( $announcement_query->have_posts() ) : ?>
<div class="prepend-top-lg">
<?php while ( $announcement_query->have_posts() ) : $announcement_query->the_post(); ?>
<?php get_template_part( 'template-parts/content', 'announcements' ); ?>
<?php endwhile; // End of the loop. ?>
</div>
<?php endif; ?>
<?php
// Get pagination for custom query
set_query_var( 'max_num_of_pages', $announcement_query->max_num_pages );
get_template_part( 'partials/partial', 'pagination' );
?>
<?php wp_reset_postdata(); ?>
</main><!-- #main -->
<?php get_sidebar( 'page' ); ?>
</div><!-- #primary -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment