0
votes

I'm searching for the best solution to create custom layouts for blog pages in WordPress.

Where you would normally select a custom page template from the Template metabox in admin to change the page layout, this does not work for static blog pages.

By default, WordPress template hierarchy is looking for home.php and then index.php to display, it does not check for custom page templates at all. View the Template Hierarchy here. WordPress codex also notes this about Blog pages:

Do not use a custom Page template for this page. The template files home.php or index.php will be used to generate this page in the Theme.

I personally find this very odd. If there is one page I would like to have different layouts available it's the blog listing.

My current and only solution is to create a new metabox called something like "Blog Layouts". My home.php then loads a custom template part based on what value is set by the metabox. Although I'm sure this will work, It feels kind of hacky knowing that WordPress provides it's own metabox for setting page templates.

If anyone has better suggestions, I'm really happy to read them.

3

3 Answers

1
votes

You could leverage the template metabox.

<?php

function get_blog_template() {
    // Get the template metabox value from the page used as post archive. 
    $template_file = get_post_meta(get_option('page_for_posts'), '_wp_page_template', true);
    if ($template_file == 'templates/blog-small.php') {
        get_template_part('templates/blog', 'small');
    } elseif ($template_file == 'templates/blog-wide.php') {
        get_template_part('templates/blog', 'wide');
    } else {
        echo 'Sorry, no template is found';
    }
}

?>

And then call the function in home.php

<?php get_blog_template(); ?>
0
votes

If the site has only pages and posts then it is OK to just edit the index.php as long as you have a page.php file for pages. There is no need to create another template file.

0
votes

You can create specific templates for specific categories.

http://codex.wordpress.org/Category_Templates

Your best bet would be to categorize the posts you want styled differently and then create a template for that category.