Skip Navigation

[Resolved] Change the content template on the fly?

This support ticket is created 4 years, 8 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by briana-5 4 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1548599

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.

#1549253

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

For example - you can add the following code to your theme's functions.php file or "Custom Code" section offered by Toolset:
=> => https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

//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.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

#1550741

Thank you for the reply. Two things.

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:

[wpv-conditional if="( '[wpv-current-user info='logged_in']' eq 'true' )"][wpv-post-body view_template="Template1"][/wpv-conditional]
          
[wpv-conditional if="( '[wpv-current-user info='logged_in']' eq 'false' )"][wpv-post-body view_template="Template2"][/wpv-conditional]

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?

#1551825

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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

#1552835

Thanks. Those are some great resources. Before I close this though I wanted to check that it's not possible to do something like this:

<em><u>hidden link</u></em>
#1553021

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

More info:
=> https://toolset.com/documentation/programmer-reference/views-api/#render_view_template

#1553031

Got it. Thanks for the insight. I'll give that a try.

#1553035

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Great - Please feel free to resolve the ticket. Have a great day. 🙂

#1559263

My issue is resolved now. Thank you!