I am trying to: use a different template depending on post category. I have 4 different venues within one website, all having their own template and header. But when I view a post I want it to pick up the specific venue header i.e template.
I tried using the following code, but didn't work, Is there an easier way to do this with shortcodes?
add_action('template_include', 'load_single_template');
function load_single_template($template) {
$new_template = '';
// single post template
if( is_single() ) {
global $post;
// 'cat-1' and 'cat-2' are category slugs
if( has_term('cat-1', 'category', $post) ) {
// use template file single-template-cat-1.php
$new_template = locate_template(array('single-template-cat-1.php' ));
}
if( has_term('cat-2', 'category', $post) ) {
// use template file single-template-cat-2.php
$new_template = locate_template(array('single-template-cat-2.php' ));
}
}
return ('' != $new_template) ? $new_template : $template;
}
I visited this URL:
I expected to see:
Instead, I got: the default template
Thank you for contacting us here in the Support Forum
This your code and goal dose not use Toolset API, GUI or features.
I can help you do this as example with Layouts Plugin.
Or by using CRED forms, and performing a similar action but assigning a certain Layout to the Post on CRED submit.
The Code you use is using only WP API and the right place to get help for it would be the WP Support Forum:
https://wordpress.org/support/
If you want to use the Layouts Plugin and have a certain Layout of your choice assigned to a certain Post depending on Taxonomy, you can use a Code similar to the Below:
function assign_layout_by_post_status( $id, $layout ){
global $post;
$preferred_layout = 12345; // my preferred layout ID
$control_layout = 0; //just set to 0 if you don't have any layout set for your post yet
if( $id === $control_layout && $post->post_type === 'your_type'){
if (has_term( 'cat-1', 'category', $post)) {
return $preferred_layout;
}
}
return $id;
}
add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);
Please do not hesitate to open a new thread if other issues or problems arise
Thank you for your patience.