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
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
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?
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.
Sorry I forgot to attach it. Super busy round here.
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.
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.
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.
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.
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.
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).
Also I believe i have the correct slug for the category that needs the different content template.
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.
What should be in the post type field? Is the attached code correct?
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.