I have created a view. I would like to use that view but be able to change the content template while viewing the page. As an example one content template displays 6 custom fields in a complex layout; the other shows just three in a more a more simplified layout.
I know that I can use javascript to show or hide certain elements in a way that could achieve this. But I'm wondering if it's possible to simply "switch" content templates from the front end in any way.
Hello. Thank you for contacting the Toolset support.
Toolset View's offers the filter wpv_filter_force_template which you can use to change the content template on fly while displaying your post on the frontend.
//Show a specific Content Template applied to a given post for not logged in visitors and in every place where this post appears:
add_filter( 'wpv_filter_force_template', 'prefix_fixed_content_for_visitors', 99, 3 );
function prefix_fixed_content_for_visitors( $template_selected, $id, $kind ) {
if ( !is_user_logged_in() && $id == 345 ) { // if the user is not logged in and is trying to view the post with ID 345
$template_selected = 123; // assign a fixed Content Template with ID 123 that contains a static text
}
return $template_selected;
}
Where:
- You can adjsut the above filter code as per your requirement.
1. Your example does't quite address the scenario that I am asking about, so I'll explain more below. But first, that example is helpful for another use case that I am considering. However, if one wanted to display a different content template based on whether the user was logged in, couldn't they just do this within the view loop:
Seems that would be easier for most Toolset users than editing the theme (that's the point of Toolset after all....). Is there a reason not to use that approach?
2. On my original question, I should have been more clear. "On the fly" was misleading. What I want to accomplish is that the front end user is presented with an option that when selected changes the content template of the page. For example, one content template loads by default that is a more complex template with more fields. They can then click a button or check box that says "Compact View" which loads a different template. Thoughts on how to accomplish that within a View?
1. Yes - that's correct. Using [wpv-conditional] shortocde to display conditional content is the easiest way.
2. On my original question, I should have been more clear. "On the fly" was misleading. What I want to accomplish is that the front end user is presented with an option that when selected changes the content template of the page. For example, one content template loads by default that is a more complex template with more fields. They can then click a button or check box that says "Compact View" which loads a different template. Thoughts on how to accomplish that within a View?
==>
There is no such feature available. Maybe you can use some custom jQuery script to accomplish this.
Please check the following reference links that may help you to implement the show/hide content based on checkbox checked/unchecked.
=> hidden link
=> hidden link
=> hidden link
Yes - it could be possible but you need to write custom shortcode to grab the URL param value to catch the template slug/id you will pass and you can use the render_view_template() function to render the content template value based on the template ID.