Move template include callback to its own function.

Add blody class filter.
Add pre get posts for front end to display 25 posts at a time
Add no sidebar class when displaying archive page.
Add getter function for template loader.
Add basic copy of twenty sixteen archive to archive teplate,. First pass twek.
Add single content template.
parent 12efaa4e
Pipeline #76 failed with stage
in 0 seconds
......@@ -37,13 +37,9 @@ class Codeup_Advent_Calendar {
*/
public function bootstrap() {
self::$template_loader = new Advent_Calendar_Template_Loader;
add_filter( 'template_include', function ( $original_template ) {
if ( CAC_ADVENT_DAY_TYPE === get_post_type() && is_archive() ) {
return self::$template_loader->get_template_part( 'archive', CAC_ADVENT_DAY_TYPE, false );
} else {
return $original_template;
}
} );
add_filter( 'template_include', [ $this, 'template_include' ] );
add_filter( 'body_class', [ $this, 'body_class' ], 10 );
add_action( 'pre_get_posts', [ $this, 'show_25_days' ] );
if ( is_admin() ) {
add_action( 'pre_get_posts', [ $this, 'sort_by_day' ] );
......@@ -87,7 +83,7 @@ class Codeup_Advent_Calendar {
switch ( $column_name ) {
case 'image':
$image = get_field( 'acd_image' );
$image = get_field( 'acd_image', $post_id );
if ( $image ) {
?>
<img class="acd-thumbnail" src="<?php echo $image['sizes']['thumbnail']; ?>">
......@@ -145,4 +141,60 @@ class Codeup_Advent_Calendar {
}
}
/**
* Show 25 days when displaying on the front end.
*
* @param WP_Query $wp_query The current query.
*/
public function show_25_days( WP_Query $wp_query ) {
if ( CAC_ADVENT_DAY_TYPE === $wp_query->get( 'post_type' ) &&
$wp_query->is_archive() ) {
$orderby = $wp_query->get( 'orderby' );
if ( empty( $orderby ) ) {
$wp_query->set( 'orderby', 'rand' );
$wp_query->set( 'posts_per_page', '25' );
}
}
}
/**
* Optionally return our archive template.
*
* @param string $original_template The current calculated template.
*
* @return string Our template or the original.
*/
public function template_include( string $original_template ) : string {
zed1_debug('original template=', $original_template);
if ( CAC_ADVENT_DAY_TYPE === get_post_type() && is_archive() ) {
return self::$template_loader->get_template_part( 'archive', CAC_ADVENT_DAY_TYPE, false );
} else {
return $original_template;
}
}
/**
* Add no-sidebar class to body classes to remove allocated space for it in 2016.
*
* @param array $classes The current array of classes.
*
* @return array array The modified array of classes.
*/
function body_class( array $classes ) : array {
if ( CAC_ADVENT_DAY_TYPE === get_post_type() && is_archive() ) {
$classes[] = 'no-sidebar';
}
return $classes;
}
/**
* Get our template loader.
*
* @return Advent_Calendar_Template_Loader
*/
public function template_loader() : Advent_Calendar_Template_Loader {
return self::$template_loader;
}
}
<?php
echo "<p>advent calendar archive template.</p>";
/**
* The template for displaying archive pages
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* If you'd like to further customize these archive views, you may create a
* new template file for each one. For example, tag.php (Tag archives),
* category.php (Category archives), author.php (Author archives), etc.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Sixteen
* @since Twenty Sixteen 1.0
*/
$options = get_option( 'cac_settings_name' );
$page_title = isset( $options['cac-calendar_title'] ) ? $options['cac-calendar_title'] : '';
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php echo esc_html( $page_title); ?></h1>
</header><!-- .page-header -->
<?php
// 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.
*/
Codeup_Advent_Calendar::get_instance()->template_loader()->get_template_part( 'content', CAC_ADVENT_DAY_TYPE );
// End the loop.
endwhile;
// Previous/next page navigation.
the_posts_pagination(
array(
'prev_text' => __( 'Previous page', 'twentysixteen' ),
'next_text' => __( 'Next page', 'twentysixteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>',
)
);
// If no content, include the "No posts found" template.
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
<h2 <?php post_class() ?>>
<?php the_title(); ?>
</h2>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment