Skip Navigation

[Resolved] Conditional output of a view based on taxonomy inside of a content template.

This support ticket is created 7 years, 3 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by Beda 7 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#563729
view b.jpg
view a.jpg

Tell us what you are trying to do?
I am trying to create a content template for a CPT that includes a view at the bottom that would conditionally show "View A" if the CPT that is being viewed is located in category "A" -or- show "View B" if the CPT is located in category "B".

Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Is there a similar example that we can see?
I have attached screenshots

What is the link to your site?
The two pages involved are hidden link (Weekly Videos)
hidden link (Training Videos)

#563799

1. Create your two Views.

2. Create your Post Type and posts in it, add a taxonomy term as well

3. Create the Content Template that you want to use for this Post Type

4. Register the custom function has_term() in Toolset > Settigns > Front-end Content > Functions inside conditional evaluations:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

4. Insert both Views and wrap each of them in a conditional.

The example code below is based on a Post Type "Toolset Types" and a Taxonomy "Toolset Taxonomy" with a term "Term Toolset".
It will show the View A only if the Post has "Term Toolset" assigned.
If there is any Term assigned it will show View B.

I crated this conditional with the GUI and the help of this DOC:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/displaying-taxonomies-conditionally/

[wpv-conditional if="( has_term('Term Toolset', 'toolset-taxonomy', null) eq '1' )" ]
 
Here your View A
 
[/wpv-conditional]
[wpv-conditional if="( has_term('Term Toolset', 'toolset-taxonomy', null) eq '1' )" ]
 
Here your View A
 
[/wpv-conditional]
[wpv-conditional if="('[wpv-post-taxonomy type='toolset-taxonomy' format='slug' ]' ne '' )"]

Here your View B
 
[/wpv-conditional]

Let me know if you need more infos.

#564415
2017-08-28-10-41-training.salesexcellence.com.png

Okay, I added the above code to a new view and inserted that view in my Content Template and it returned nothing. I've attached a screenshot so you can see my settings for this new view.

Here is my breakdown:
Custom Post Type = Training Videos
Toolset Taxonomy for Training Videos = Categories
Terms inside Categories that I want to apply conditional statements to = weekly-videos, video-library
IF taxonomy 'Categories' has the term 'weekly-videos' THEN show view 'Weekly Video Listing'
IF taxonomy 'Categories' has the term 'video-library' THEN show view 'Video Library Categories Parent'

#564681

Does it display something if the conditional is removed?

If yes, your conditional checks on the wrong data.

You could then check the data saved for each post and make sure your condition is correct, or let me check this.

I will need the exact links to the View, the Front End, the Posts saved (some of them) and also some of the Terms.

Thanks.

#565031

Basically I'm trying to have 2 different displays of the same CPT using the same content template based on what category it belongs to.

This is not really related to your initial Query, you wanted to show a View A and B, conditionally, and this is to do in ONE Content Template only.

Now, if you want to display a Post with a Content Template depending on it's Taxonomy, you would need Custom Code.
Ways to assign the Content Template to the Post depending on the Taxonomy are:
1. Update the meta field _views_template with the ID of the Content Template.
This is done in PHP
2. Call the content template with the URL parameter ?content_template=40 (where 40 is the ID of your Content Template)
This URL parameter can be added to the Post URL, so it would always call the Content Template you pass in it.

Now, in a View, you can use the Conditional HTML, but if your Post IS in a specific Term, it will always be in that term, even if there is one other term assigned.

If you want to call ONLY posts in ONE specific Term you need a add the condition that checks if the Term is assigned:

[wpv-conditional if="( has_term('Term TITLE', 'taxonomy-slug', null) eq '1' )" ]
  
[/wpv-conditional]

If you want to make sure that the post is NOT in any other taxonomy, you need to check for each term, there is no other way to do this.

has_term checks if this one specific Term is assigned, but not if any other is or is not assigned.

So, given your setup, this is not possible

The best would be, to switch to a Custom Field that as example holds values like "Show in A" and "Show in B".
Then you can use that on the conditional