Skip Navigation

[Resolved] Conditional statements based on view shortcode attribute or NOT empty

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

Problem: I have a View that displays different results based on a shortcode attribute. I would like to use the output of this View in a conditional. If the results are not empty, I would like to show a text header.

Solution: Avoid using a View's results in a conditional. You'll run into lots of problems with extra spaces, markup, and nested attribute quotation marks, causing the conditional to fail. Instead, use the View's wpv-items-found block to your advantage and display the text here. You can pass arbitrary text into a custom shortcode attribute, and display it in the View using the wpv-attribute shortcode.

View shortcodes:

[wpv-view name="degree-listings" wpvposttag="academic programs, FST, major" heading="Majors"]
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, masters" heading="Masters"]
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, cas" heading="Certificates of Advanced Study"]

Loop output:

[wpv-layout-start]
    [wpv-items-found]
<strong>[wpv-attribute name="heading"]</strong>
    <!-- wpv-loop-start -->
    <wpv-loop>
        your loop stuff goes here
    </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]No items found[/wpv-no-items-found]
[wpv-layout-end]

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-309292
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

This support ticket is created 4 years, 10 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 2 replies, has 2 voices.

Last updated by Suzanne Wenger 4 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1266653

I would like to display conditional text based on the results of a view shortcode with wpvposttag attributes assigned.

In one of my pages I am filtering and loading views based on assigned taxonomies:
hidden link

Here is an example of the shortcode tag loading a filtered view:
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, major"]

What I would like to do is add conditional text if the view is not empty but nothing I am trying is working:
[wpv-conditional if="( '[wpv-view name='degree-listings' wpvposttag='academic programs, FST, major']' ne '' )"]Majors[/wpv-conditional]

[wpv-conditional if="(NOT(empty( '[wpv-view name='degree-listings' wpvposttag='academic programs, FST, major']' )) )"]Majors[/wpv-conditional]

The view [wpv-view name="degree-listings-by-level"] is basic and is used over and over in multiple places within the site and even multiple times on one page in many cases. What I want to avoid is the problem of having to create many views just for adding text in certain locations. If I can use the results of a view shortcode in a conditional statement within specific pages or in a parent view it would save me from having to do this.

For more context...below is additional page code as an example:

<h2>Academic Offerings by Degree</h2>
<h3>Majors</h3>
[wpv-view name="degree-listings" wpvposttag="major"]

<h3>Accelerated Programs</h3>
[wpv-view name="degree-listings" wpvposttag="accelerated"]

<h3>Masters Programs</h3>
[wpv-view name="degree-listings" wpvposttag="masters"]

<h2>Degrees by Department</h2>
<h3>Food Studies</h3>
Majors
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, major"]
Masters
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, masters"]
Certificates of Advanced Study
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, cas"]

The above text within the tags need to be conditional based on the results of the views below them because if no degrees are displayed from the view "degree-listings" then the heading text is listed but with nothing below it. I need to change the text above to something like this:

<h2>Degrees by Department</h2>
<h3>Food Studies</h3>
[wpv-conditional if="( '[wpv-view name='degree-listings' wpvposttag='academic programs, FST, major']' ne '' )"]Majors[/wpv-conditional]
[wpv-view name="degree-listings-by-level" wpvposttag="academic programs, FST, major"]
[wpv-conditional if="( '[wpv-view name='degree-listings' wpvposttag='academic programs, FST, masters']' ne '' )"]Masters[/wpv-conditional]
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, cas"]
[wpv-conditional if="( '[wpv-view name='degree-listings' wpvposttag='academic programs, FST, cas']' ne '' )"]Certificates of Advanced Study[/wpv-conditional]
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, cas"]

#1267023

Hi, Views aren't really meant to be used as the criteria for a conditional this way, and you're very likely to run into maximum nesting depth problems especially if you try to use another shortcode inside the View attribute. If I were trying to set this up, I would avoid conditionals if possible. Instead, use an arbitrary shortcode attribute in the View shortcode to set the header text, then output that text above the loop tags using the wpv-attribute shortcode. Here's an example (note the "heading" attribute):

[wpv-view name="degree-listings" wpvposttag="academic programs, FST, major" heading="Majors"]
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, masters" heading="Masters"]
[wpv-view name="degree-listings" wpvposttag="academic programs, FST, cas" heading="Certificates of Advanced Study"]

Then in the Loop Output editor:

[wpv-layout-start]
	[wpv-items-found]
<strong>[wpv-attribute name="heading"]</strong>
	<!-- wpv-loop-start -->
	<wpv-loop>
		your loop stuff goes here
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]No items found[/wpv-no-items-found]
[wpv-layout-end]

You can see more information about the wpv-attribute shortcode here:
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-309292
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#1267703

Christian...Thanks!
What an elegant solution. It worked perfectly and I learned something new about the system. 🙂

Thanks so much for your time,
Suzanne

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