1

Trying to create a 'switching' blog template using home.php, controlled by 'Blog posts per page' setting in the admin area. If set to '1', it will show the single post template. Anything higher and it will display the archive template.

In detail:

The following code works, except for archive pagination (clicking older/newer posts changes the URL but displays first page content only).

<div id="primary" class="content-area">

<h2>Switching Blog Template</h2>

<?php   // Get ppp for query
        $ppp_val = get_option( 'posts_per_page' ); ?>

<?php 
// Single post loop

$blogsingle_query = new WP_Query( $ppp_val <= 1 );

if($blogsingle_query->have_posts()) : 
while($blogsingle_query->have_posts()) : $blogsingle_query->the_post();

get_template_part( 'parts/content', 'post' );

endwhile;
wp_reset_postdata();
the_post_navigation();
endif;
?>

<?php
// Multiple posts loop ( PAGINATION DOESN'T WORK )

$blogmulti_query = new WP_Query( $ppp_val > 1 ); 

if($blogmulti_query->have_posts()) : 
while($blogmulti_query->have_posts()) : $blogmulti_query->the_post();

get_template_part( 'parts/content', 'archive' );

endwhile;
wp_reset_postdata(); 
the_posts_navigation();
endif;
?>


</div><!-- #primary -->

In comparison, this alternate method does not work but feels like it might be a better track to follow?

<div id="primary" class="content-area">

<h2>Switching Blog Template</h2>

<?php $ppp_val = get_option( 'posts_per_page' ); ?>

<?php 
$args=array(
  'posts_per_page' => $ppp_val, // Get number of posts value
  'meta_query' = array(
    array(
      'key' => 'posts_per_page', // Use this to compare
      'compare' => '>',
      'value' => 1,
      'type' => 'NUMERIC',
    )
  ),
);
$switch_query = new WP_Query( $args ); 
?>

<?php 
if($switch_query->have_posts()) : 
while($switch_query->have_posts()) : $switch_query->the_post(); 

get_template_part( 'parts/content', 'archive' ); 

endwhile; else: 
?>

<?php get_template_part( 'parts/content', 'post' ); ?>

<?php endif; ?>

</div><!-- #primary -->

2 Answers 2

1

You can control what template loads for any type of query via the Template Filters.

Here's an example using home_template that checks if posts_per_page is equal to 1, and loads single.php in that case.

function wpd_home_template( $home_template = '' ){
    if( get_option( 'posts_per_page' ) == 1 ){
        $home_template = locate_template( 'single.php', false );
    }
    return $home_template;
}
add_filter( 'home_template', 'wpd_home_template' );
0
0

Thanks to Milo, whose answer was perfect. I hadn't encountered Template Filters before.

For the benefit of future readers, my final code (below) has been amended to reference both templates required.

function wpd_home_template( $home_template = '' ){
    if( get_option( 'posts_per_page' ) == 1 ){
        $home_template = locate_template( 'single.php', false );
    }
    elseif( get_option( 'posts_per_page' ) > 1 ){
        $home_template = locate_template( 'archive.php', false );
    }
    return $home_template;
}
add_filter( 'home_template', 'wpd_home_template' );

Over and out!

-A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.