Skip Navigation

[Resolved] Creating a different page template for certain categories based on taxonomy

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Content Template assigned to single Location posts. If the post has the term "Seasonal" applied in the Location Services taxonomy, I would like to display a different Content Template.

Solution: Use the following custom code to programmatically switch Content Templates automatically based on post type, term assignment, and type of Content Template.

//Apply a specific Content Template to posts in a specific post type with a specific term from a specific taxonomy
add_filter( 'wpv_filter_force_template', 'ts_force_template_for_seasonal', 99, 3 );
   
function ts_force_template_for_seasonal( $template_selected, $id, $kind ) {
    $post_type = 'location';
    $term_slug = 'seasonal';
    $taxonomy_slug = 'taxonomy-slug';
    $alternate_template_id = 123;
     
    // do not edit below this line
     
    if ( get_post_type( $id ) == $post_type && has_term( $term_slug, $taxonomy_slug, $id ) && $kind == 'single-location' ) {
        $template_selected = $alternate_template_id;
    }
    return $template_selected;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 19 replies, has 2 voices.

Last updated by jessicaM-4 5 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1238262

Hello Christian,

I double checked everything and it still is not working properly.

#1238265

Check now. You deleted a line of code after the "if" line. I added it back:

if ( get_post_type( $id ) == $post_type && has_term( $term_slug, $taxonomy_slug, $id ) && $kind == 'single-location' ) {
		$template_selected = $alternate_template_id;
    }

If you keep a copy of these theme files on your local machine, make sure you overwrite your local file with the file on the server.

#1238269
Screen Shot 2019-05-01 at 11.57.16 AM.png
Screen Shot 2019-05-01 at 11.55.54 AM.png
Screen Shot 2019-05-01 at 11.53.27 AM.png

Hello Christian,

Is the attached code correct. I do seem to have it working and using the correct content template. But it seems to break the template page ( See the two attached images. The one with the blue footer is correct. ). Not sure why this happens? Also strange thing it only seems to happen with certain seasonal locations.

#1238282

Seems okay now, maybe you need to flush the Divi cache? Or show me a URL where it's not working. I just tested Smoaks.

#1238312

My issue is resolved now. Thank you!