I am currently modifying an existing theme. I’d like to dynamically adjust the page template based on certain conditions. I am doing this by adding a filter for ‘template_include’ and this is working fine, meaning the original template setting for a specific page gets overwritten and it then uses “special-page.php” instead of the default “page.php”:
add_filter( 'template_include', 'special_page_template', 99 );
function special_page_template( $template ) {
if ( is_page( 'test' ) ) {
$new_template = locate_template( 'special-page.php' );
return $new_template ;
}
return $template;
}
However I have run into the problem that both templates use a common header, which has certain conditional outputs based on the call is_page_template('special-page.php')
in it. Even though I have switched the template (by adding the filter) to “special-page.php”, the is_page_template() call isn’t recognizing this change and is apparently still picking up “page.php” from the database instead. What call would be the appropriate call instead to get the name of the actual template currently in use?
My question is, which is better for working with custom templates? What are the pros and cons of both solutions?
]]>Situation: I want to have multiple search landing pages, each for a different post type (e.g. a separate search page for posts, clubs, and events).
My solution: I was going to add the following code into my functions.php file. I check if the incoming query is a search, and depending on if it’s from the “articles”, “club”, etc., form then I would redirect the page to a certain template.
Problem: The only problem is I don’t know the file path to the Elementor templates to use load_template(). I’ve looked high and low on the internet and in my wp-content folder but still couldn’t find anything. I also tried using the shortcode Elementor provides to the template but that didn’t work.
I appreciate all help! Also, if anyone knows another way to do this, please let me know.
//HTML Form
<form role='search' method='get' class='search-form' action='https://chess-intellect.local' id='sidebar-search'>
<input type='search' class='search-field' placeholder='Search...' name='s' />
<input type='hidden' name='search-type' value='articles' />
</form>
// redirect searches from form to a template
add_filter( 'template_include', 'search_redirect');
function search_redirect() {
if(is_search() && isset($_GET['search-type'])) {
$type = $_GET['search-type'];
if($type == 'articles') {
// I want to use load_template() but can't :(
do_shortcode('[elementor-template id="1818"]');
} else if($type == 'clubs') {
// load clubs template
}
}
}
]]>
add_filter( 'template_include', 'custom_template');
function custom_template( $template ) {
if ( is_page( 66 ) ) {
$template = get_query_template( 'template-test' );
}
return $template;
}
Everything work fine outside the fact I dont get the class “page-template-template-test.php” in the body class. I instead get “page-template-default”
From what I understand, template_include is called after the body tag so body_class dosent get to work with the new template. Is it normal?
Am I using the function properly?
Thank
]]>Thanks for such great plugin.
There is a filter “pre_get_posts” applying a multiple post types “post and mec-events”. Since we need to unify the search page into one template.
So I have add a “pre_get_posts” filter to restore the post type to “mec-events” only.
But there is a filter “template_include” which force to load “archive-mec-events.php”.
Since we are working for a unified search page, I suggest that in parser.php line 143,
elseif(is_post_type_archive($PT) &&)
change to
elseif(is_post_type_archive($PT) && !is_search())
so that it only load the template when it is not a search page since is_search() detect by checking the ‘s’ query variable.
Just a suggestion because if the search page is also forced a custom template, it might break the consistency of the website.
]]>Any recommendation?
]]>I’ve come across an issue where when visiting a store page the template_include hook isn’t firing in another plugin. When viewing any page other than a store page it works fine.
I’ve been logging entry to the callback to see when it fires, and as I say, on the store pages it does nothing.
Is there something in the template engine that is blocking these calls?
Thanks
Phil
If I try to include a different page template via the template_include hook, it doesn’t work within the customizer preview.
I took this example from the Codex:
<?php
function portfolio_page_template( $template ) {
if ( is_page( 'portfolio' ) ) {
$new_template = locate_template( array( 'portfolio-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
add_filter( 'template_include', 'portfolio_page_template', 99 );
For this example my “portfolio-page-template.php” only displays the word “test”:
<?php
echo "test";
?>
If I visit the portfolio page on the frontend it loads the template successfully and displays the word “test”.
But when I try to load the same URL within the customizer via the api it doesn’t loads the template and shows me the home page instead:
wp.customize.previewer.previewUrl.set('https://localhost:8888/portfolio/')
The same happens if I click on a link within the previewer to the portfolio page. It doesn’t load the template nor the page. Any other link or page that doesn’t hook into the template_include loads without a problem.
]]>I installed a child theme and now I’d like to add an html code into header.php
I used this:
function myheader( $template ) {
if ( locate_template( 'header.php' ) != '' ) {
return untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'header.php';
}
return $template;
}
add_filter( 'template_include', 'myheader', 11 );
It replaces the header and shows only the header and not the full template (the content, sidebar, footer…)
]]>