Skip Navigation

[Resolved] Conditional output referencing a post related to the current post

This support ticket is created 6 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
- 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/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by Luo Yang 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#918119

Tell us what you are trying to do? I've got two post types: an Essay and an Author. When viewing an Essay page, I want to display information related to the Author. I know I can display fields using the syntax [types field='field' item='@author.parent'][/types]. I'd also like to use conditional syntax that references the parent post. The pseudo-code below doesn't seem to work:

[wpv-conditional if="( $(wpcf-title) ne '' )"]
<p>[types field='title' item='@faculty-author.parent'][/types]</p>
[/wpv-conditional]

Is there any documentation that you are following? No

Is there a similar example that we can see? No

What is the link to your site? hidden link

#918170

Hello,

You can use the Types shortcode within attribute "if", for example:

[wpv-conditional if="'[types field="title" item="@faculty-author.parent"][/types]' ne '' )"]
<p>[types field='title' item='@faculty-author.parent'][/types]</p>
[/wpv-conditional

More help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

#918324

This is exactly what I was looking for. Thanks, Luo!

#918339

Uh oh, I may have spoken too soon.

Neither the syntax that you'd provided nor the instructions for checking for the existence of a parent post at https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/#checking-if-a-parent-exists, worked on my site. I'm pretty sure I've done something wrong, but I'm not sure what.

Here are both versions of my code:

#1 checking for the first name field of the parent post.

[wpv-conditional if="'[types field="first-name" item="@faculty-author.parent"][/types]' ne '' )"]
<p>By <a href="[wpv-post-url item="@faculty-author.parent"]">[types field='first-name' item='@faculty-author.parent'][/types] [types field='last-name' item='@faculty-author.parent'][/types]</a>, [types field='title' item='@faculty-author.parent'][/types]</p>
[/wpv-conditional]

#2 checking for a parent post ID.

[wpv-conditional if="(NOT(empty($(_wpcf_belongs_faculty_id))))"]
<p>By <a href="[wpv-post-url item="@faculty-author.parent"]">[types field='first-name' item='@faculty-author.parent'][/types] [types field='last-name' item='@faculty-author.parent'][/types]</a>, [types field='title' item='@faculty-author.parent'][/types]</p>
[/wpv-conditional]

Here's a test post: hidden link

The layout I'm modifying is the "Template for Essays": hidden link. The code is in the first row ("Subpage Image, Written by, Published on").

Can you take a look?

#918581

Thanks for the details, there is an extra ")" in my previous answer, I have modified it from:

[wpv-conditional if="'[types field="first-name" item="@faculty-author.parent"][/types]' ne '' )"]

To:

[wpv-conditional if="'[types field="first-name" item="@faculty-author.parent"][/types]' ne '' "]

Please test again, check if it is fixed or not.

#918603

Luo, thanks for your reply, and for finding the error in my syntax.

I also discovered that the reason the comparison wasn't working is because, when no Author is associated with the Essay, the value of [types field="first-name" item="@faculty-author.parent"][/types] wasn't the empty string, it was one blank space (" "). Here's the updated working code:

[php]
[wpv-conditional if="'[types field="first-name" item="@faculty-author.parent"][/types]' ne ' ' "]
[php]

Is that supposed to be correct?

Thanks again!

#918690

No, it is not supposed to be correct, unless you have saved a space ' ' in the "first-name" field.