2

In my most recent project, I'm dealing with a website that included dozens of custom taxonomies, couple of post types, content, etc. and required different templates for different authors or tags.

Right now, my template folder has about 70 php files for templates, which is really confusing.

I noticed that some themes such as twentyseven manage to store template files in folders and call them in a loop as the following:

get_template_part( 'template-parts/post/content', get_post_format() );

But this is in the loop. My templates are entirely different so i can't use the above solution, because i will need to use conditionals for altering anything that is not part of the loop.

For example, if i have 3 post types, i have to save 3 template files:

single-type1.php, single-type2.php and single-type3.php.

These templates are entirely different, both in or outside the loop (even different sidebars), so i can't just make a single.php and call the appropriate post type in the loop.

Is there anyway to address WordPress about custom template files other than just saving it directly inside the theme's folder?

2 Answers 2

2

Page templates can be stored within the page-templates or templates subdirectory within a theme, but this does not apply to custom post type or taxonomy templates.

Fortunately, the template_include filter can be used to change the template that will be loaded. In the example below, template files are stored in the /theme-name/templates/ directory.

/**
 * Filters the path of the current template before including it.
 * @param string $template The path of the template to include.
 */
add_filter( 'template_include', 'wpse_template_include' );
function wpse_template_include( $template ) {
    // Handle taxonomy templates.
    $taxonomy = get_query_var( 'taxonomy' );
    if ( is_tax() && $taxonomy ) {
        $file = get_theme_file_path() . '/templates/taxonomy-' . $taxonomy . '.php';
        if ( file_exists( $file ) ) {
            $template = $file;
        }           
    }


    // Handle post type archives and singular templates.
    $post_type = get_post_type();
    if ( ! $post_type ) {
        return $template;
    }

    if ( is_archive() ) {
        $file = get_theme_file_path() . '/templates/archive-' . $post_type . '.php';
        if ( file_exists( $file ) ) {
            $template = $file;
        }
    }

    if ( is_singular() ) {
        $file = get_theme_file_path() . '/templates/single-' . $post_type . '.php';
        if ( file_exists( $file ) ) {
            $template = $file;
        }
    }

    return $template;
}
5
  • Thanks dave. So, basically if i save singular.php inside /theme-name/templates/, will the WordPress automatically load it as if it's in the theme's root itself?
    – Johansson
    Apr 21, 2017 at 7:56
  • Sure, Jack! The code I posted does work the way you describe for specific single post, post type archive, and taxonomy archive templates. E.g. single-type1.php, archive-type1.php, and taxonomy-tax1.php templates placed in /theme-name/templates/. Apr 21, 2017 at 18:54
  • The code does not handle the generic singular.php template (although it would be possible to override that template too, as well as any other one). Personally, I think it's overkill to mess with the location of the general templates outlined in the hierarchy, but I see the advantage of relocating the templates for unique objects since they can grow numerous as you've described. Apr 21, 2017 at 18:55
  • Thanks dave. I mean without this function, will WordPress first check /templates/` folder for any template or not? Obviously it will with the provided code.
    – Johansson
    Apr 22, 2017 at 3:24
  • 1
    You got it Jack. Nope, the /templates/ directory will not be checked without this code. /templates/ was used as an example, so you can change it to whatever path you'd like. Apr 22, 2017 at 9:05
3

You could use 1 template for all taxonomies and name it taxonomy.php or use template_include in your functions file.

add_filter( 'template_include', 'tax_page_template', 99 );

function tax_page_template( $template ) {

    if ( is_tax( 'taxonomy' )  ) {
        $new_template = locate_template( array( 'your-tax.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

Depending on which theme you are using, you could also use 1 template vis the Page Attributes > Templates drop down menu and modify each template using theme settings.

Your Answer

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

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