14

I want to create a new custom page template for the presentation of blog posts. I realise that when you assign a page to display posts via Settings > Reading, it then will assign home.php to display that page content - being posts on a page. Also the option to assign a page template in the page editing view disappears.

Right now it displays a default article by article view. I want to provide another option for admin to select a different template which could style the blogs in a gird or masonry layout. I'm not sure how to do this.

I could do it by not assigning the page to display blogs in the Settings > Reading admin area and then allowing admin user to assign the specific template to page using Page Attributes menu in page editing but according to https://make.wordpress.org/themes/2014/06/28/correct-handling-of-static-front-page-and-custom-blog-posts-index-template/, this is the WRONG way.

I've tried google searching for solutions but all I keep finding is recommendations for plugins to use. Could someone point me to some helpful resources?

1
  • I guess the original issue is long since resolved now but I just wanted to say that reading through the source code of plugins has taught me a lot. If you can find a plugin on the WP.org directory you can click the developers tab to view the code straight from the browser, and see how does it.
    – rtpHarry
    Feb 12, 2020 at 16:00

3 Answers 3

16

Don't forget that Wordpress was primarily designed to be a blogging CMS, so when it comes to theme development, developers often opt for a non-standard approach in exchange for the potential for more features.

Theme developers have three options when they approach this, one of which (#2 below) you mentioned.

  1. Directly edit the index.php to modify the blog index. This is not a good option because index.php should be the a fallback in case another part of your template is missing.

    • Pros: fast and easy
    • Cons: error prone and against object oriented principles
  2. Create a page template for the blog index. Like you said, many theme developers elect to go this route because it's a fast way to give you control over the blog index, and it actually gives you the ability to play around with different blog index templates (which is useful when developing a versatile theme).

    • Pros: Versatile, allows for building a robust theme
    • Cons: you lose the ability to call Wordpress' native functionality that pertains to the blog index.
  3. Create a front-page.php, home.php and index.php in your theme. The front-page will be the home page for the theme. home will default to your blog index and index will be your fallback for all templates.

    • Pros: Clean and makes full use of Wordpress' native objects and methods
    • Cons: Limited by Wordpress: not ideal for many of the kinds of option-rich themes you see today

Personally I like to go with #2, because most of my Wordpress development projects these days are not just blogs: they're entire sites with deep information architecture and complex interactivity.

1
0

Going to cover several bases in my answer. :)

If you are creating your own theme, and want to change the default presentation of some types of pages (e.g. the category archive page, or a single blog post page), then check out the diagram of the WordPress template hierarchy. It shows which files in your theme directory are used to display which types of post.

By having a php file in your theme that matches a file in that hierarchy, it will be automatically used by WordPress—such as single.php for displaying a single post. If you have a theme with that file already there, then obviously you can change the file and it will be reflected on the site.

Separately, if you want to have custom page templates but only want an admin to be able to change them (on a post-by-post basis), then you need to do some checks on roles and capabilities; this StackExchange answer may be helpful. (You'd still need to create those custom page templates, see this Smashing Magazine article).

Otherwise, if you want an admin to more globally choose page/post templates based on certain criteria, or to choose custom page templates for the entire site, then you'll need to add an admin-area option to your theme, and some custom code in your theme to display the right page based on that option. Adding an option to the admin area is easily done with one of the many plugins but also can be done by hand (see, for example, this article on SitePoint).

3
  • 1
    Thanks for your answer. I'm aware of how to make page templates for pages/posts based on the heirachy but I can't figure out a way to give template options for "Blog Posts Index Page" which is using home.php (using same terminology as diagram you provided). This is set when an admin user chooses a page to display posts in Dashboard > Settings > Reading. That then assigns the page home.php/index.php. I cannot figure out a way to make an alternate template without NOT assigning the page in Dashboard > Settings > Reading. Jan 4, 2017 at 5:26
  • @RyanCoolwebs You would probably need to add an admin option to your theme that allows a choice between display styles for the blog posts index page, and then edit home.php to read the value of the option and display the appropriate page variation.
    – iguanarama
    Jan 4, 2017 at 7:23
  • Yes, I think using the customiser feature is probably the best way of actually doing this. I can check on customiser options and then retrieve the appropriate template part. Jan 4, 2017 at 7:44
0

Ok, you mean you want to create your own template page to show blog posts, right ?

Then go this way create a php file named blog-template.php

Inside that file put this code on top of every thing.

<?php 
/*
* Template Name: Blog Post
*/
?>

Then below above given code, put all your code from index.php/home.php page to this above page, save the file and now create a page from dashboard, say as name of that page is "Blog" Now from right side chose page template which we just created as Blog Post from that drop-down of templates.

Publish the page and you are good to go for having your own page to show your posts.

Let me know if any thing else needed.

2
  • OK, so you've just refrased the solution that OP clearly stated as wrong ;) Creating page template and using it as blog index isn't the best approach, I guess. Mar 18, 2018 at 17:27
  • It's not really wrong per se, just that OP misunderstood why people tend to do it this way
    – Orun
    Jul 17, 2018 at 20:32

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