Take the WordPress route

How I manage multiple custom layouts with template routing.

I use what I call WordPress routing, I have not looked to see if this is actually a thing or not. To me this is a civilized approach to the template hierarchyThe default way would be to create template for all of your pages by the end you have a messy root theme folder.

Since WordPress by default is going to run everything through index.php, page.php, tag.php, and category.php we can use a the index.php file as a route by leveraging is_front_pageis_search, and is_home.

if ( have_posts() ) {
    if ( is_front_page() ):
        get_template_part('templates/content', 'home');
    elseif( is_search() ):
        get_template_part('templates/content', 'search');
        if ( file_exists(get_template_directory().'/templates/content-'.$post->post_name.'.php')):
            get_template_part('templates/content', $post->post_name);
        elseif ( is_home() ):
            get_template_part('templates/content', 'blog');
            get_template_part('templates/content', 'page');
} else {
  get_template_part( 'templates/content', 'none' );

If a request is made to an About page the request goes to page.php and would then load templates/content-about.php

I can create templates on the fly without needing to go into the admin page and associate the template with the content. This of course is subjective, Matching one template to multiple pages would not work in this case. 

I have had great success with this method so far.

Signup for my mailing list

Receive other rambings like this on design, code, and some times food.