0

One of the best way to override the default template hierarchy is to use the child theme and copy and paste the template file (from parent theme to the child theme) you want to override and change the code of the template file according to your need (to display the content on the template page). The other way is to use filter ( add_filter( 'template_include' , 'custom_template' , priority_value );) to bypass the predefined structure of template hierarchy.

Can any body explain how the second method works in detail?

1

To understand how this works, take a look at the source file template-loader.php. Here you see several lines of statements which determine which template WordPress will load. Near the end is the filter, which allows you to undo all the previous lines.

For instance, one of the lines determines which template to load if is_single is true. You could use the filter to add extra conditions, for instance to load a different template if is_single is true and the post is in a certain category. Beware, this still means you would need to have a template file in your child theme (where you also have the filter in fucntions.php).

Basically, for all templates that WordPress can find through the conventional naming of the template hierarchy, it doesn't make sense to use the filter. You would only use it if very specific conditions apply under which the template must be called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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