Using the WordPress template hierarchy with the Customizr theme

WordPress has a theme template system in the form of a top to down hierarchy. When WordPress loads any page, it looks at the theme files and if there is a file for the specific page that is loading, it uses that. If there isn’t, it will look for the next template file down the hierarchy. The last template file on the hierarchy is index.php.

For more detail please refer to the official  Template Hierarchy documentation by WordPress.

The goal of the following documentation is to explain, with simple examples, how the WordPress template hierarchy can be implemented with the  Customizr Theme

Please not that the following are not complete customization examples that you can copy and paste into your theme or child theme.

Overview

This template hierarchy is useful when you want to customize your theme.

Following this user question : From what I understand, I need a single-*.php in my theme folder, though that's a guess - I have no idea why the content is appearing. Do you have a single-*.php doc available, or will I need to create one?

In the  Customizr Theme, there is only index.php for loading content. Since everything fallback to index.php, that is why custom post type content will appear, even if you don't have a custom post type content template. By looking at the hierarchy for the single post template.

We can see that there is 4 types of template that precedes index.php.

How to create a single-{post-type}.php template?

In the user question, the user is referring to the single-{post-type}.php template. This is a template that shows your article content according to post type. This is commonly used for creating a different article content layout in a custom post type. We will not be discussing custom post type here, it's a huge topic.

  1. Create a Child Theme.
    • Your newly created template should reside in a child theme, so that it does not get overwritten during a parent theme update.
  2. Duplicate your index.php file and name it according to your post type.
    • For example, if your registered post type is product. You should name it single-product.php
    • For example, if your registered post type is acme_product. You should name it single-acme_product.php
  3. Note: It's very important to stick to the naming convention of the template.
    • For example, if registered post type is acme_product, you cannot name your template single-acme-product.php, WordPress will not be able to find it.
    • Do not include the curly brackets, they are for indicating post type or slug.
  4. Open up your newly created template. Made a simple edit to the template.
  5. Upload your newly created template to your child theme folder. It should be in the root folder, same level as style.css
  6. Create a custom post from your WordPress admin and view it on your front end.
  7. You should be seeing your custom post type article in your new template.

How to create a special category template?

Let's say you have a category named News, and you want to be able to have a different layout for this category. By looking at the category template hierarchy. We can see that there is category-{slug}.php

  1. You should already have your child theme.
  2. Duplicate your index.php file and name it category-news.php
    • Remember to stick to the naming convention, it will not work, if you name it categorynews.php or cat_news.php
    • It's has to be your category slug, not your category name. It will not work, if you name it category-News.php
  3. Open up your newly created category-news.php template. Made a simple edit to the template.
  4. Upload your newly created category-news.php template to your child theme folder. It should be in the root folder, same level as style.css
  5. Visit your News category on your blog, it will be using your newly created template.

.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.