If I visit a page on my WordPress site, WP goes and determines what template file it should use based on the template hierarchy.
If I’m doing some logic checking in the hook init
and say my conditions are met, how do I tell WordPress to load a template file of my choosing, not the one WordPress would normally dish up?
Can you perform the logic on the
template_include
filter? Else, you could set a global/constant and use that intemplate_include
to serve up the appropriate template.The template_include filter filers the path to the template file to be included. To avoid errors you should check if the template exists – locate_template does this for theme/child-theme files.
I suppose you could use some conditions like this:
Then obtain parts of your template depending on the section of your website using
get_template_part();