I'm wondering if there is a way to display a content template in a view conditionally. For example, on the home page of this site, I am using a view that's loading a content template for posts. What I would like it to do is to see which content template is being used by the post and then display the correct one. So, if I have a post that is on a content template called "Post Template", I want it to display one content template in my view. If I have a post that is on a content template called "Quote", I want it to display a different content template in my view.
Is it better to do this based on category instead, for example? One content template for "Post Template" in category "Cooking" and another for "Quote Template" in category "Quote". And then, how do I make that distinction in my view?
Is there any documentation that you are following?
No. I haven't done anything on it yet, because I can't quite figure it out.
Is there a similar example that we can see?
Nope. If I could find one of those I could definitely reverse engineer this. Sorry!
What is the link to your site?
hidden link
Sure, you can display almost anything conditionally in a view, more complex is to display something based on any condition.
To display anything, you just wrap what you want to display conditionally in an HTML Condition like explained here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
You can base your conditions on Types and Views Fields, or custom ShortCodes, but it's more complex to base those conditions on data that is, for example, a whole bunch of strings (Content Template, Post Body, etc).
The scenario you outline would be possible to achieve by getting the post meta field of the post where we (Toolset) stores the ID of the Content Template assigned to it.
That is a hidden field "_views_template".
You can show it with this shortcode:
[wpv-post-field name='_views_template']
This means, in an HTML condition you can listen to that field and then load conditionally that content template.
[wpv-conditional if="( '[wpv-post-field name='_views_template']' eq 'Your Template ID to check' )"]
Place here whatever should be output in case the condition is true
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-field name='_views_template']' eq 'Your Other Template ID to check' )"]
Place here whatever else be output in case the first condition is false but this one is true
[/wpv-conditional]
Note that you need to use the ID (not the title or name) of the Content template since _views_template will return you the ID of the assigned Template
I'm however not sure on the purpose of that, as usual, you may just display the Post Body of the Post with the very content template you choose why inserting the Post Body ShortCode, but there are sure use cases of what you request in more complex sites.
Above would be the solution to it.
Okay, this is great feedback. So, it would be preferable to have this based on a category, yes? I can definitely do that, since that seems to be the most straightforward way to go. And right now the client has, essentially, two content templates and they will be based on categories (ie. I could set this up to display the main content template for any post that is not in the category of "Quote".)
Can you just let me know if I'm on the right track with that? Does that sound wrong to you? If not, I'll just stride confidently forward until I blow up her website trying to make the conditional display. 🙂
Thanks so much!!
Oh! Yes, one more question: if I set that up based on category, would I choose to not automatically assign a content template to the post? Or would it be okay to do that still?
Many thanks!!
Hi again. I tried this, both with the taxonomy and the content template and got a big 'ol fail. What's the next step? Can you see what I did wrong?
Wit the code below it's now showing nothing but the sidebar (as in, there's something wrong with the code so it is choosing to output nothing): hidden link
Here is the code:
[wpv-conditional if="( '[wpv-post-field name='_views_template']' eq 'Post Layout-Short-Template' )"]
<div class="sb-home-short">[wpv-post-body view_template="Post Layout-Short-Template"]</div>
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-field name='_views_template']' eq 'Quote Template' )"]
<div class="sb-home-short">[wpv-post-body view_template="Quote Template"]</div>
[/wpv-conditional]
Hello,
Please provide a test site with the same problem, also point out the problem page URL, where and how can I see the problem:
both with the taxonomy and the content template and got a big 'ol fail.
What are you expecting to see the output?
Thanks for the details, I assume we are talking about the view:
hidden link
Please check above view, in section "Loop Editor", you can get the content template ID with shortcode:
[wpv-post-field name='_views_template']
Test it in front-end, you should be able to see it outputs number 62, which is ID of your content template "Post Layout Template":
hidden link
So you can setup your codes like this:
[wpv-conditional if="( '[wpv-post-field name='_views_template']' eq '62' )"]
<div class="sb-home-short">[wpv-post-body view_template="Post Layout Template"]</div>
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-field name='_views_template']' eq '66' )"]
<div class="sb-home-short">[wpv-post-body view_template="Post Layout-Short-Template"]</div>
[/wpv-conditional]
[wpv-conditional if="( '[wpv-post-field name='_views_template']' eq '110' )"]
<div class="sb-home-short">[wpv-post-body view_template="Quote Template"]</div>
[/wpv-conditional]
Hope it is clear
This didn't fix the problem, and ultimately broke more things on the page, so I'm just going to leave it for now. I do want to say that the first guy who helped me, Beda, was super great. Thanks Beda!