0

I'm creating a Wordpress Theme, and I want to create few Page Templates for creating Blog pages with different layouts. So for example, I want to have blog-4-column.php, blog-3column.php and blog-list.php, for giving the user 3 different layout options for having a Blog Page. Now I'm able to list blog posts in my index.php file using this code:

if (have_posts()) : while(have_posts()) : the_post(); ?>
  <?php get_template_part('content', get_post_format()); ?>
<?php endwhile; else : ?>
  <div id="post-<?php the_ID(); ?>" <?php post_class('no-posts'); ?>>
      <h3>No Posts found</h3>
  </div>
<?php endif;

Where I have content.php, content-link.php, content-gallery.php to display posts of different Post Formats. Now the only way I know how to do this in a page template is by using WP_Query, as the post type in the page template would be 'pages' and using the above code will list the registered Pages and not Posts.

I just want to know if there is anyway I can use the same 'content-post-format' files for this, as I don't want to create 3 or 4 'content-post-format' files for each individual Blog Page Template that I would create.

I've searched around for this and one I found most useful was on Tutsplus, but still it doesn't cover having different 'content-post-format' files and also having few different Page Templates for listing Blog posts in different ways.

Please let me know about this and thank you.

2
  • I'm not sure I understand your question - you can reuse the same template parts, as get_post_format depends on the current $post object in the loop, which will work the same whether it's the main query or a custom query.
    – Milo
    Commented Apr 14, 2014 at 16:15
  • I think you understood it correctly. I just don't know how to use the same content-* files for displaying posts in different layouts, depending on which Page Templates is calling that file. So for example, if blog-3-column.php template calls content.php, then I want to display the posts in 3 columns, and if blog-list.php calls the same content.php, then I want to display the posts in the lists layout. I just don't want to create different content.php files for different Page Templates. Hope its clear and thank you for answering. Commented Apr 14, 2014 at 16:24

1 Answer 1

1

I wouldn't use custom page templates merely for changing the layout of the blog posts index. Using page templates for the blog posts index bypasses the core handling for display of the blog posts index (which per the template hierarchy uses either home.php or index.php to render).

Edit

I never...said that I want to change layout of the blog posts 'index' in my question.

Yes, that's exactly what you're talking about. In WordPress nomenclature, the blog posts index is the default listing of all blog posts, whether on the site front page, or a static page. When you configure Settings -> Reading -> Front Page Displays as a static page, and then assign a static page to Posts page, then WordPress uses that assigned static page to display the blog posts index.

When WordPress uses that page to display the blog posts index, it ignores any assigned page templates. It ignores the page.php template. It always and only uses either home.php or index.php.

Thus, if you force the user to use a custom page template to display the blog posts index, then you're breaking the way that core functionality works. In order for users to be able to use your custom page templates, they have to create yet another static page, and then assign your custom page template to that page. The users either have to unassign the page assigned to Posts page in Settings -> Reading, or simply ignore that the page exists, and duplicate its output on the separate static page to which they have assigned your custom page template.

Original Solution

Instead, define a Theme option for blog layout, then use that option value to conditionally include template-part files, conditionally display sidebars, or to filter body_class (or post_class) for CSS changes.

Conditional Template-Part Files

$theme_options = get_option( $option_name );

get_template_part( 'content', $theme_options['blog_layout'] );

Conditional Sidebars

$theme_options = get_option( $option_name );

if ( 'full' != $theme_options['blog_layout'] ) {
    get_sidebar();
}

Filter body_class

function wpse141248_filter_body_class( $classes ) {

    if ( is_home() ) {
        $theme_options = get_option( $option_name );
        $classes[] = 'layout-' . $theme_options['blog_layout'];
    }
    return $classes;    
}
add_filter( 'body_class', 'wpse141248_filter_body_class' );

Filtering post_class would be essentially the same technique.

6
  • Thank you for answering. But I want to give theme users the option to have a static page as the Home page of the site, and also allow them to have a different page for showcasing their blog posts. Now I can still probably use Theme Options for this, but I also want to create a demo page in which I'll show the different Blog layouts options, which I believe wont be possible if I take the theme options approach, and thus I would need to create different pages using different Page templates. Commented Apr 14, 2014 at 16:41
  • "But I want to give theme users the option to have a static page as the Home page of the site, and also allow them to have a different page for showcasing their blog posts." WordPress has this feature out-of-the-box, by configuring Settings -> Reading -> Front Page Displays. By using a custom page template for the blog posts index, you short-circuit this core functionality. Instead, you should support the core functionality. Commented Apr 14, 2014 at 16:48
  • "..but I also want to create a demo page in which I'll show the different Blog layouts options..." - that's a separate issue, that can/should be handled separately. You shouldn't change the way that users configure core options, merely to facilitate a Theme demo. Commented Apr 14, 2014 at 16:49
  • I'm aware of the Wordpress Functionality of setting the front page of the site, and the only reason why I mentioned it is because I thought you had miss-understood my question. I never mentioned 'Front Page' or said that I want to change layout of the blog posts 'index' in my question. I don't want to change the way the Front page is displayed at all, and it can be whatever the user likes, by configuring it through the Settings, as you mentioned. All I want to do is create few Page templates for displaying the Blog posts in different layouts, but still use the same content-*.php files. Commented Apr 14, 2014 at 17:01
  • "I never...said that I want to change layout of the blog posts 'index' in my question." - please see my edited answer that explains this point. Commented Apr 14, 2014 at 17:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.