I have cars that are available for SALE and some that are ALSO available for LEASE. I set a checkbox to mark which cars are also available to lease. The same car can be available for SALE and for LEASE.
I have one Content Template to display information for the single car for SALE.
I want to display those same cars that are also available for LEASE under the leasing section of our site, but I want to display them differently, with some of the same specs, but I have different literals that I need to display about leasing terms on the Content Template, and a different layout.
I know that I can display different layout criteria with a CONDITIONAL, but sometimes I need the car to output as a SALE layout, and the same car to output as the LEASE layout, so the either/or conditional based on what is stored for the car won't work.
Is it possible I can use a conditional in the "Lease car View" or test for which VIew output the car?
The bottom line is how can I have two different Content Templates for the same single post type record?
Hi, the best way to handle this is to use our Views API Hook wpv_filter_force_template to programmatically apply a specific Content Template, based on the value of the post's lease custom field. We have documentation for this API here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template
In your child theme's functions.php file, or in a new code snippet in Toolset > Settings > Custom Code, you can add code like this:
//Show a specific Content Template applied to a given post if a custom field value is 1:
add_filter( 'wpv_filter_force_template', 'tssupp_sale_and_lease_template', 99, 3 );
function tssupp_sale_and_lease_template( $template_selected, $id, $kind ) {
if ( $kind == 'single-car' && get_post_meta( $id, 'wpcf-lease', true ) == 1 ) { // if this is a single 'car' post with 'lease' field value of 1
$template_selected = 123; // assign a fixed Content Template with ID 123
}
return $template_selected;
}
In the example above, you would replace car with the slug of the custom post type, and lease with the slug of the lease custom field. Of course, if your lease field has a value other than 1 you would replace 1 with that value. You would also change 123 to match the numeric ID of the Content Template you want to apply to Sale + Lease cars.
Let me know if you have additional questions about that, or if the code isn't working as expected.
Hi, thank you for the quick reply - and that snippet is very powerful and does work as programmed.
The only problem is, sometimes I have to display it as a SALE car too. The above snippet displays that content template ALL the time, whether it's on the SALES end of the website, or over on the LEASING section. I need to display the same single record, in two different ways, depending on what section of the site the visitor is on.
I have one VIEW for SALES and a separate view that filters for those cars that have the LEASE flag set.
On the SALES part of the website, I need to display the car as for SALE, with layout 1.
On the LEASING part of the website, I need to display the same car record, as for LEASE with layout 2.
So, is there a way, to choose the layout based on what VIEW the user linked over from?
So, is there a way, to choose the layout based on what VIEW the user linked over from?
You want to display a different template based on where the User clicked the link to see the single Car post? To do that, you could append the content-template-id URL parameter to the post link in the View, and set the appropriate template ID without needing any extra custom code:
<a href="[wpv-post-url]?content-template-id=12345">[wpv-post-title]</a>
Thank you SO much - this is precisely what I needed. Sorry if I wasn't more clear in my original post. This saves me dozens of hours!