Skip to content

Views: Template Hierarchy

alex_prokopenko edited this page Jan 17, 2018 · 3 revisions

The standard template system is updated into views. It means that all templates are moved to views folder. Furthermore, all templates have been grouped in one more directory level by section.

So, in general, all template files are located like this:

/views/{section}/ .. files ..

{section} is a post type ID or logical group (like search)

By default we have several pre-defined view section names:

├── views/                  # → Templates
│   ├── layouts/            # → Template wrapper templates
│   ├── partials/           # → Small template parts same for several sections. (Header, footer, etc.)
│   │
│   ├── page/               # → Page templates (like front-page, page-*, etc.)
│   ├── post/               # → Post templates (like index, archive, single, etc.)
│   └── search/             # → Search templates

All template names follow standard WordPress Template Hierarchy namings.

Let's check how standard Template Hierarchy works with views system:

Post templates

You can use such template name patterns to create some unique template:

├── views/                  # → Templates
│   ├── post/               # → Post templates
│   │   ├── index.php           # → Default for blog listing or if other templates missing
│   │   ├── archive.php         # → Default for blog archives if category/tag/author/date templates missing
│   │   │
│   │   ├── category-{slug}.php
│   │   ├── category-{id}.php
│   │   ├── category.php
│   │   │
│   │   ├── tag-{slug}.php
│   │   ├── tag-{id}.php
│   │   ├── tag.php
│   │   │
│   │   ├── date.php
│   │   │
│   │   ├── author-{nicename}.php
│   │   ├── author-{id}.php
│   │   ├── author.php
│   │   │
│   │   ├── single-{slug}.php   
│   │   └── single.php          # → Default for post single  

Page templates

You can use such template name patterns to create some unique template:

├── views/                  # → Templates
│   ├── page/               # → Page templates
│   │   ├── front-page.php     # → Default for front page if Static homepage is enabled
│   │   ├── 404.php             # → 404 error template
│   │   │
│   │   ├── {custom template file}.php
│   │   ├── page-{slug}.php
│   │   ├── page-{id}.php
│   │   └── page.php            # → Default for any page   

Search templates

├── views/                  # → Templates
│   ├── search/               # → Search templates
│   │   └── search.php            # → Search results/No results template   

Custom post types

All custom post type templates will be placed in separate folder. Standard WordPress template names will be still available:

├── views/              # → Templates
│   ├── {cpt ID}/           # → Custom post type templates
│   │   ├── archive.php     # → Custom post type archive
│   │   ├── index.php       # → Custom post type archive - alternative
│   │   │
│   │   ├── single-{slug}.php   
│   │   └── single.php      # → Custom post type single template   

In our practice we found that standard archive pages are not very useful: you cannot control the page title, description, featured image, etc. Everything is hard-coded in the template or theme options and it's not straightforward for the site administrator to edit this content.

We recommend creating custom template files with custom query/custom loop to display custom post type archives. In our development we usually place custom template file for CPT archive inside CPT templates folder.

Custom taxonomies

For custom taxonomies the default template will be:

├── views/   
│   ├── {cpt ID}/archive-{taxonomy ID}.php

Example: If you created taxonomy "department" and attached it to CPT "employee", then the correct template is views/employee/taxonomy-department.


Next: Views: Layouts