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
#1237334

Tell us what you are trying to do?
I have regular locations and seasonal locations. The seasonal locations have far less info than the regular locations. So I need to have a different view/template for those to accommodate the lack of content.
Is there any documentation that you are following?
Toolsets
Is there a similar example that we can see?
This is and example of a regular location ( hidden link ). This is an example of seasonal link ( hidden link ). You can see why I need to create a different template for seasonal.
What is the link to your site?
hidden link

#1237376

Hi, there's not an easy way to do this in wp-admin, but the PHP API wpv_filter_force_template can help. Here is an example that applies a different Content Template based on whether or not the current post is in a specific post type and also includes a specific term from a specific taxonomy:

//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 )) {
        $template_selected = $alternate_template_id;
    }
    return $template_selected;
}

Change location to match the post type slug if necessary. Change seasonal if necessary to match the slug of the term that represents a seasonal property. Change taxonomy-slug to match the slug of the custom taxonomy that contains the seasonal property term. Change 123 to match the numeric ID of some unassigned Content Template. Then paste this code in your child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom Code.

The documentation for this API and other Views filters is available here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

#1237871

Hello again Christian,

I have added that code to the child themes functions file ( see attached screenshot ). Please look over and see if I input the correct information. Also Im a little confused on how to apply the Seasonal template i created to the locations with the seasonal category. I guess that what this code is supposed to do?

#1237883

I didn't get an attachment, sorry. The code will automatically apply the alternate template based on the post type and the term assigned in the custom taxonomy, regardless of what template is chosen in wp-admin. When you make the required changes, the code handles everything else.

#1237884
Screen Shot 2019-04-30 at 12.53.54 PM.png

Sorry I forgot to attach it. Super busy round here.

#1237914

1. "Location" is not the slug of your custom post type. Slugs do not have capital letters. You can determine the slug by going to Toolset > Post Types and editing the post type. Go into wp-admin and get the real slug, and replace it.

2. I think you have the term slug and the taxonomy slug switched around. The term slug should be the slug of whatever term you're using to define a seasonal property. The taxonomy slug is the slug of the taxonomy that holds the seasonal term.

#1238172
Screen Shot 2019-05-01 at 9.31.25 AM.png
Screen Shot 2019-05-01 at 9.31.16 AM.png

Good morning Christian,

I have added the code and it seems to break the map page ( See screenshot ). Would you be able to take a look at the code and the page to see what im doing wrong. Im attaching screenshots of both.

#1238185

Okay we need to add a conditional to restrict the scope of this template change. Find this line in the code:

if ( get_post_type( $id ) == $post_type && has_term( $term_slug, $taxonomy_slug, $id )) {

Change it to this:

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

Let me know if the map issue is still unresolved, I'll take a closer look.

#1238217

Hello Christian,

The map issue did resolve but it still not working properly. It does not seem to be calling the page template of the ID I entered in. When I look at seasonal locations it is still showing the old content template.

#1238219

Instead of guessing about information like slugs, you should get the actual values from wp-admin. Go to Toolset > Taxonomies and edit the Location Services taxonomy. Find the actual slug, and replace the incorrect slug you have in your code.

#1238227
Screen Shot 2019-05-01 at 11.06.47 AM.png
Screen Shot 2019-05-01 at 11.06.16 AM.png
Screen Shot 2019-05-01 at 10.59.59 AM.png

Hello Christian,

Following your instructions it looks like the slug is location-services ( See attached image ). I have that added in the code under the $taxonomy_slug. Are you saying this also needs to be the $term_slug as well ( See attached image )? I believe I have the post type slug in there correctly but look and make sure ( See attached).

#1238245
Screen Shot 2019-05-01 at 11.09.32 AM.png

Also I believe i have the correct slug for the category that needs the different content template.

#1238252

No, I'm not saying this needs to be $term_slug as well, it is only $taxonomy_slug. You should check the Seasonal term slug and confirm it is "seasonal". If not, change the $term_slug to match the actual slug of the Seasonal term. Go to Locations > Location Services and look for the Seasonal term to confirm this. Then test out the changes and see if you get the results you expect.

#1238254
Screen Shot 2019-05-01 at 11.14.18 AM.png

What should be in the post type field? Is the attached code correct?

#1238260

What should be in the post type field?
Already told you how to set this up: https://toolset.com/forums/topic/creating-a-different-page-template-for-certain-categories-based-on-taxonomy/#post-1237914

Is the attached code correct?
Maybe, maybe not. You won't know for sure until you test it. Test it and tell me if it works as expected.