Skip Navigation

[Resolved] Adding Conditional Code To Retrieve Taxonomy Item In A View

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View that displays a custom post type. I have a custom taxonomy associated with this custom post type. If the term "New" is associated with a post in the View, I would like to display "New" with the post information. No other terms should be displayed for this post.

Solution: The wpv-post-taxonomy shortcode is not filterable or conditional like this, so you must use a nested View of this taxonomy. Add a term Query Filter, set by the current post in the loop. Then in the loop, use the wpv-taxonomy-title shortcode in Conditional HTML to test each term in the results. If the term matches "New", display the term. If not, display nothing.

[wpv-conditional if="('[wpv-taxonomy-title]' eq 'New')"]
New
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 5 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 2 voices.

Last updated by Christian Cox 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1379143

Tell us what you are trying to do?
I have the following code in a view that is part of a wider set of code that displays a list of custom post titles then next to each one, a taxonomy term. There are two terms (is that the right word for them) in the list, *New* and *Featured*. I wish to only show the taxonomy if it is *New*, so how can I write what I think is a conditional something or other to say ‘if this post has a tick for the *New* taxonomy item, then put it here, or don’t put anything?

<wpv-loop>
[wpv-post-link]
[wpv-item index=other]<font style='color:#fc92dc'>[wpv-post-taxonomy type="girl-promotion" format="name"]</font> <font style='color:#000000'>-</font>
[wpv-item index=last]<font style='color:#fc92dc'>[wpv-post-taxonomy type="girl-promotion" format="name"]</font> <font style='color:#000000'></font>
</wpv-loop>

Many thanks,
Barry.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site? hidden link The view in question is the horizontal list of girl's names in the footer.

#1379451

I wish to only show the taxonomy if it is *New*, so how can I write what I think is a conditional something or other to say ‘if this post has a tick for the *New* taxonomy item, then put it here, or don’t put anything?
Hi, there's not an easy way to do this with the wpv-post-taxonomy shortcode because it's not highly configurable. Instead, you would create a new View of this taxonomy and add a filter by term, where the term is set by the current post. Eventually you will nest this View in the View of custom posts to make it dynamic. In the loop of this View of taxonomy terms, you will add conditional HTML that tests the current term. If it matches the "new" term, display the term or term archive link or whatever. If not, display nothing.

If you need more information about the conditional part, we have documentation available here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
Let me know if this doesn't make sense and I can try to offer some additional guidance.

#1381257

Hi Christian,

I've tried to do this without further help but I'm stuck. I think I understand what you mean but I also think I'm missing something. I've added the following conditional within the view (a new one I've created from scratch) and to me the original view query correctly finds all girls with another taxonomy ticked called 'onsite'. So that works as the girls names are listed. The conditional, added just after the post name (where I want the term to show), says ' find the custom posts with the taxonomy 'Girl Promotions with term *New* but it doesn't to me say, if you find them, show the text *New* and if the post doesn't have *New* as a term, then show nothing. So I think I'm missing that last bit. Am I getting there? If you can nudge me in the right direction or tell me what I need to do I'd be grateful.

I do understand that rather than show the term itself automatically, I might need to physically write the term out in text in the conditional.

<wpv-loop>
[wpv-item index=other]
<font style="color:#000000;">[wpv-post-title][wpv-conditional if="( CONTAINS(#(girl-promotion),'*New*') )"], </font>
[wpv-item index=last]
<font style="color:#000000;">[wpv-post-title]</font>
</wpv-loop>

(I've left out index-last for now).

Many thanks,
Barry.

#1381505

I'm thinking something like this for the conditional:

[wpv-conditional if="('[wpv-taxonomy-title]' eq 'New')"]
New
[/wpv-conditional]

Does that make sense in your case? The wpv-taxonomy-title shortcode is used to represent the name of the current term in the loop.

#1381893

Hi Christian,

Thank you for your help but perhaps this is something that can't be done. I don't want to waste any more of your time so I've removed the view for now and I'll try to revisit the idea if I can later on. I'm very grateful for you trying though. Many thanks,
Barry.

#1382141

I'm sure it can be done, and you're definitely not wasting my time. I'm here to help, and I'm sorry this is discouraging. I don't want to give up. If you'd like, I can log in to the site, take a closer look, and set things up for you in a test area. Then we can review the changes I made before you add them to the live area. If you'd like to collaborate, please provide admin login credentials in the private reply fields here in the forum.

#1382389

Okay please take a look at this new View I created:
hidden link

This is a View of the 'Girl Promotion' taxonomy, filtered by term, where the term is set by the current post in the loop. This means if I insert this View in another View of posts, only the terms associated with the current post in the loop will be displayed.

In the Loop of this View, I added the conditional to display only the term "*New*". This means that even if other terms from this taxonomy are associated with the current post, they will not be displayed.

Next, I removed the conditional content from your original list View and inserted the new View instead:
hidden link

This technique of nesting views is useful when you want to make lists within lists...in this case lists of terms in lists of posts.

Let me know if you have additional questions about that.

#1382391

Hi Christian,

That's truly brilliant. I'm going to have to study what you did carefully until I understand it fully but it's absolutely perfect so thank you very much for all the effort.

Many thanks,
Barry.

#1382393

My issue is resolved now. Thank you!

#1382397

Excellent, feel free to reach out here in the forums anytime you get stuck.