Skip Navigation

[Resolved] Connitional display based on view output

This support ticket is created 5 years, 6 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
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 10 replies, has 2 voices.

Last updated by Christian Cox 5 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1259973

Conditional output seems to be restricted to parameters that are related to the current post. I am trying to make decisions that are related to the current logged in user. I am therefore using a view to get the information related to the logged in user and evaluating its output e.g. [wpv-conditional if="( '[wpv-view name="mentorm-display-shortlist-status-within-profile-page"]' eq 'Peer mentor' )"]. However, this never seems to be evaluated. I have used this format to evaluate shortcodes successfully. Am I doing something wrong or is there another way to do this?

#1260201

Normally a View outputs extra markup and spaces that will cause this conditional to fail. Luckily it's not necessary in most cases. If you're looking for a conditional based on a custom field in the current logged-in User's profile, you can use the Types field shortcode directly in the conditional without the need for another View.

[wpv-conditional if="( '[types usermeta="user-text-1" current_user="true"][/types]' eq 'Peer mentor' )"]

go!

[/wpv-conditional]

If you must use another View, you should eliminate all line breaks and spaces from the View, and also check the "Disable the wrapping div around the results" checkbox in the View editor.

#1260299

Thanks Christian
Unfortunately the information I am looking at is in a custom post type and not the user's profile. I had the div disabled and the output is a single simple line of text from a single types field with no breaks or spaces. The condition still fails. I have also been trying to detect when the view produces no results by looking for the "No items found" string. That comparison also fails. Any options occur to you?

#1260371

First I would move this shortcode outside of the conditional and check the page source to see what it outputs:

[wpv-view name="mentorm-display-shortlist-status-within-profile-page"]
#1260391

I did that and it output the simple strings I expected such as "No items found" and the single status message I expected.

#1260477

May I log in to wp-admin and see how this is set up? Let me know where I can find the View on the front-end of the site.

#1261213

Well the first thing I noticed is that you still have lots of spaces and line breaks in this View's loop output area:
hidden link
The contents there should all be on one long unbroken line, with no spaces in between shortcodes. That's the first thing I would change.

I did that and it output the simple strings I expected such as "No items found" and the single status message I expected.
Check the page source. The "No items found" message is not a simple string, it is wrapped in HTML markup and surrounded by spaces. Similarly, when results are found you'll have a lot of extra spaces around the text. The conditional engine will not be forgiving here, you must remove all line breaks and spaces around the output.

#1262423

Christian
I condensed the code and removed all spaces and line returns from the view coding. I wass able to get a conditional statement to recognize "No items found" if I included one line return in the comparison string as follows.
[wpv-conditional if="( '[wpv-view name="mentorm-display-shortlist-status-within-profile-page"]' eq '

No items found' )"]it worked for shortlist[/wpv-conditional]

Having to do this is arkward and I would suggest some recoding on the Toolset end so that all of this is not required.

However, the saga continues. For views that include a view parameter this does not work. In fact, the debugger will not even display. I have added a test section to the bottom of the hidden link page so you can see all this in action.

Any suggestions on how I might detect the No items found result for these more complex views?
Regards
Graham

#1264503

You're right, it's awkward and I think we can do better. In simple cases you may be able to take advantage of the wpv-items-found and wpv-no-items-found blocks in the Loop Output. For example, if you want to display some content only if a View returns results, you can leave the wpv-loop tags empty and place your desired content somewhere between the wpv-items-found shortcodes. If you want to show something else if the View produces no results, place that content in between the wpv-no-items-found shortcodes. Here's a basic example:

[wpv-layout-start]
	[wpv-items-found]

This content will only be shown if the View produces results. The wpv-loop tags are empty so nothing will be repeated or looped over. It's a simple conditional that takes advantage of the Loop Output structure.


	<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		
This content will only be shown if the View produces NO results. You can modify the "No items found" message to use your own custom shortcodes or HTML.

	[/wpv-no-items-found]
[wpv-layout-end]

This is a simple example, but it's powerful in that you can still use View shortcode attributes effectively, and there's no need for the conditional syntax. The View's filter and Loop handle that part for you.

For a more robust solution, you're probably going to need to create custom shortcodes using the Types and Views APIs. Let me know if you would like to dig in there.

#1264519

Christian
Could you address the second part of my issue about the debugger not working for views that include a view parameter.

Thanks
Graham

#1265571

There's nothing wrong with using shortcode attributes in a View per se. The debugger is not working because your view and conditional syntax is invalid.

[wpv-conditional if="'[wpv-view name="mentorm-display-outgoing-request-and-response-status-only" postauthor=[wpv-post-author format="meta" meta="user_login"]]' eq '

No items found' )" debug="true"]it worked for requests sent[/wpv-conditional]

At first glance, I can see there are no quotation marks after postauthor=, and there are double quotation marks nested directly inside other double quotation marks in the post author shortcode. Both of these are illegal in syntax. Regardless, these View-results conditional clauses are not ideal, as you have already noticed. Plus there are limits to the number of nested quotation marks that can be supported here, you can't keep nesting attributes inside attributes inside attributes.