PressTigers

Extension of Template Hierarchy

At some stage of template development, we might need to develop a template which is not available by following the template hierarchy provided by WordPress. One option is that we can put a condition into our template code and display that part on becoming the condition true. However, the standard solution is extending template hierarchy.

We may need to have a different template for a single post on the basis of a category which is not possible with the default template hierarchy of WordPress. For example, we can have a single template for custom post type by creating template file name as “single-cusotmpost.php” because “single-catslug.php” or “single-catid.php” are not allowed. But we can extend the template hierarchy to achieve category based single template by adding single_template filter. To add this filter, follow the steps mentioned below:

  • Create a file name as single-catslug.php
  • Add filter like add_filter(‘single_template’, ‘single_cat_template’);
  • Create function single_cat_template to handle template name processing and redirection
  • Add the following code into single_cat_template function:

User Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Get in Touch