Skip Navigation

[Resolved] Conditional statement in Views isn't working as expected

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 8 replies, has 2 voices.

Last updated by Waqar 10 months ago.

Assisted by: Waqar.

Author
Posts
#2643779

Hi there,

I've set up two views: one view gets a list of taxonomy terms from a specific taxonomy. The second (child) view gets a list posts with that taxonomy term and creates a slider.

Here's a conditional in the child view that is trying to detect the presence of a specific taxonomy term:

[php]
[wpv-conditional if="( CONTAINS(#(curatorial-category),'2023 International Creative Aging Summit') )" debug="true"]
<div class="homepage_curatorial_category_resource_grid international_creative_aging_summit_2023">
[/wpv-conditional]
[wpv-conditional if="( NOT(CONTAINS(#(curatorial-category),'2023 International Creative Aging Summit')) )"]
<div class="homepage_curatorial_category_resource_grid">
[/wpv-conditional]
[php]

This conditional is located inside a <wpv-loop> tag.

What it's doing, or trying to do, is looking for a specific taxonomy term ("2023 International Creative Aging Summit") in the taxonomy named "curatorial category." If the term is present in this iteration of the loop, the div will include an extra class.

At the moment, two curatorial category names are including the extra class:

+ 2023 International Creative Aging Summit (correct!)
+ Research (NOT correct)

I've reviewed the debugging output from the condition, and here it is for the two matching categories:

1. 2023 International Creative Aging Summit (correct!)
####################
wpv-conditional attributes
####################
Array
(
[if] => ( CONTAINS(#(curatorial-category),'2023 International Creative Aging Summit') )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( CONTAINS(#(curatorial-category),'2023 International Creative Aging Summit') )
--------------------
After matching 2 numeric strings into real numbers: ( CONTAINS(ARRAY('2023-international-creative-aging-summit',1954,'2023 International Creative Aging Summit'),'2023 International Creative Aging Summit') )
Matched '1954' to 1954

2. Research (NOT correct)
####################
wpv-conditional attributes
####################
Array
(
[if] => ( CONTAINS(#(curatorial-category),'2023 International Creative Aging Summit') )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( CONTAINS(#(curatorial-category),'2023 International Creative Aging Summit') )
--------------------
After matching 2 numeric strings into real numbers: ( CONTAINS(ARRAY('2023-international-creative-aging-summit',1954,'2023 International Creative Aging Summit','research','1646','Research'),'2023 International Creative Aging Summit') )
Matched '1954' to 1954
After matching 5 numeric strings into real numbers: ( CONTAINS(ARRAY('2023-international-creative-aging-summit',1954,'2023 International Creative Aging Summit','research',1646,'Research'),'2023 International Creative Aging Summit') )
Matched '1646' to 1646

It seems as if an array is being substituted into the conditional expression. I really only need the taxonomy term name. It also appears to be the case that in the second instance, *two* taxonomy terms are present in the conditional. I don't understand why that's happening, it's not what I was expecting / supposed to happen.

My objective, to be clear, is to loop through the list of taxonomy terms and check if one of the terms is "2023 International Creative Aging Summit."

Can you offer any guidance?

Thank you.

Saul

#2644295

Hi Saul,

Thank you for contacting us and I'd be happy to assist.

Instead of using two separate conditional statements, you can use only one:


<div class="homepage_curatorial_category_resource_grid [wpv-conditional if="( CONTAINS(#(curatorial-category),'2023 International Creative Aging Summit') )"]international_creative_aging_summit_2023[/wpv-conditional]">

Div container's content..............

</div>

This example works on my test website and in this approach, only the class itself is wrapped inside the conditional statement.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2644419

Waqar,

Thanks for your response. I tried revising the condition per your code, and the issue persists on my end. A close look at the debugging statements indicates there's something wrong with the conditional statement.

On most taxonomy terms, like the one below ("Online Programming"), the comparison is computed as follows with a single item (or single group, really, of a taxonomy slug, taxonomy ID, and taxonomy name) in the array:

( CONTAINS(ARRAY('online-programming',1562,'Online Programming'),'2023 International Creative Aging Summit') )

But for one term, "Research," the array is always computed differently:

( CONTAINS(ARRAY('2023-international-creative-aging-summit',1954,'2023 International Creative Aging Summit','research',1646,'Research'),'2023 International Creative Aging Summit') )

As you can see, there are actually *two* groups of terms in the array above ("Research" and "2023 International Creative Aging Summit").

What's the next step to troubleshoot?

Saul

#2644775

Thanks for the update and it is strange that it is still not working as expected.

Can you please share temporary admin login details, along with the example link where you're testing these conditional statements? I'll also need your permission to download a clone/snapshot of the website, in case it needs to be investigated on a different server and at the database level.

Note: Your next reply will be private and it is recommended to make a complete backup copy before sharing the access details.

#2645993

Thank you for sharing these details.

I noticed that the conditional statements were working correctly, but the div container that was being targeted wasn't the right one.

The div container with class 'homepage_curatorial_category_resource_grid' is the wrapping container, which is started at the start of every row.
( it is a 3-column layout where this container is opened before the first item of the view and closed after the third one )

This means that if the first item of the view has that term and the condition becomes true, then all the other two column items will also show in that same background color.

The conditional check, therefore, needs to be moved inside the individual result's loop item, with a div having class 'homepage_curatorial_category_resource'.

I've moved the conditional statement inside the loop item and included the relevant custom CSS code and this is fixed.
( screenshot: hidden link )

Note: You'll see that the first card under 'Research' is still showing in the beige background and that is because it does have the term '2023 International Creative Aging Summit' attached too.

#2646217

Waqar,

Thank you for your response and for updating the code on the server. Your reply says that the conditional is in the wrong place. I don't think it is. Each taxonomy category is its own row, and each row contains three or more posts. Here's a simplified view of the hierarchical structure:

+ category 1
	+ post 1.1
	+ post 1.2
	+ post 1.3
+ category 2
	+ post 2.1
	+ post 2.2
	+ post 2.3
+ category 3
	+ post 3.1
	+ post 3.2
	+ post 3.3
...

I want all posts in a specific category / row ("2023 International Creative Aging Summit") to have a beige background. Therefore it makes sense to add the CSS class to the desired row. I think, at the moment, the condition tests each post to check the category that it's in. But I want to check the *category* itself, not the post's category.

Let me try to explain further. The view that outputs each row is a child view. There's a parent view ("View of Homepage Curatorial Categories", ID 5861) that iterates over each taxonomy term. How can I refer to the parent taxonomy name in the child view, and include that in the conditional?

I hope that makes sense.

Saul

#2646561

Thank you for clarifying that.

For this particular requirement, you can target the current taxonomy's title value in the parent taxonomy view's loop through the '[wpv-taxonomy-title]' shortcode.

Through this conditional check, you'll basically add a special container around each instance of the child view.
( screenshot: hidden link )

I've moved the conditional check from the child view to the parent one and adjusted the CSS code accordingly and it is working now.

#2646649

Waqar,

Thanks for your response and for sharing the screenshot. That makes sense. I'll copy your code to my production site.

One follow-up question: is there a way to refer to the taxonomy term name in the child view? I'm just curious.

Thanks!

Saul

#2647025

Yes, you can pass the taxonomy term name from the parent view's loop to the nested child view, as a view's shortcode attribute:
https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/#accessing-the-arguments

The passed value can be accessed in the child view through the 'wpv-attribute' shortcode:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-attribute

#2647567

Ah, excellent. Thanks so much for all of your help, Waqar!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.