Skip Navigation

[Resolved] Use parent's post slug inside a conditional statement

This support ticket is created 5 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by Nigel 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1220090

Inside a View, which is added to a page, I need to use a conditional statement to check if the value of a custom field matches the value of the parent page slug.

Here's what I have: If 'select-page' custom field is not equal to an empty string AND 'select-page' IS EQUAL to the current page's slug, we found a match.

[wpv-conditional if="( $(wpcf-select-page) ne '' ) AND ( '[wpv-post-slug]' eq $(wpcf-select-page) )"]
[types field='select-page'][/types]
[/wpv-conditional]

I really think I am just lacking the correct syntax that is needed, '[wpv-post-slug]'.

#1220274

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Benjamin

The issue is how to test the slug of a parent page rather than the current page, right?

If you are literary talking about hierarchical pages, then you would do so like so:

[wpv-post-slug item="$parent"]

If you are actually talking about relationships created with Types and you want to refer to the parent from a child then you need the relationship slug, which you would use like:

[wpv-post-slug item="$relationship-slug.parent"]
#1220516

That doesn't work there seems to be a syntax error when using what you wrote in the conditional statement.
Here's the code now:

[wpv-conditional if="( $(wpcf-select-page) ne '' ) AND ( '[wpv-post-slug item="$parent"]'  eq $(wpcf-select-page) )"]
	[types field='select-page' output='raw'][/types]
[/wpv-conditional]

Anything after $parent is in a different color, neon orange, which I think means there is a syntax error
"]' eq $(wpcf-select-page) )"]

#1220801

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sorry, you need to be careful about quotes.

The whole of the if condition should appear inside one set of quotes, i.e. double-quotes in this case, meaning all of the internal quotes that appear inside the condition need to be single quotes.

Try changing the quotes around $parent to item='$parent'

#1221097

The single quote fixed the syntax error thank you

I now have a few new issues. $parent isn't what I needed but $currentpage, why that is I don't know. What I am trying to do is match the parent's page slug, the page the View is being added to, with the RAW name of the field inside a conditional statement. The reason I want to use the slug instead of the title is my client could possibly change the Title of the page but the slug will be left alone. So, now the issue is, again probably syntax, I don't know how to add the raw attribute to my field inside a conditional.

With these I am getting the correct data I am trying to match
[types field='select-page' output='raw'][/types]
[wpv-post-slug id="$current_page"]

Here is my new conditional not sure how to add the RAW attribute correctly to the field.

[wpv-conditional if="( $(wpcf-select-page) ne '' ) AND ( '[wpv-post-slug item='$current_page']' eq $(wpcf-select-page output='raw') )"]
	[types field='select-page' output='raw'][/types]
[/wpv-conditional]

Is there a way to setup a time for a Chat session? In 5 minutes I think we could have this worked out but this is taking days. Everytime I try to use Chat it converts my question into another support ticket are there select times when chat is live and actually works? I'm 10 hours behind Greenwich GMT10

thanks

#1221114
#1221539

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2019-03-27 at 11.13.01.png

Hi Benjamin

Sorry, I can't initiate chat, one of our supporters (50%) in your time zone has been off sick so far this week which is probably why you were unable to get chat support when you tried.

Regarding the issue, you've been describing the slug of the parent page, but it seems like it's not the parent, it's just the current page where the View is inserted. Parent refers to a related post or page, and I believe you mean just the current page, which is why you needed to use "$currentpage".

Try adding debug="true" to your wpv-conditional shortcodes to see what is being evaluated and compared.

Check my screenshot for an example.

I have a select custom field "status" which has options such as "Accepted", "Rejected" etc. where the raw values are in lowercase ("accepted", "rejected", etc.).

I added this to my template:

<p>Status [types field='status'][/types] ([types field='status' output='raw'][/types])</p>

[wpv-conditional if="( $(wpcf-status) eq 'Accepted' )" debug="true"]
<p>Accepted!</p>
[/wpv-conditional]

You can see that I output the field display value then the raw value (=> "Accepted (accepted)") then added a conditional which tests this field using $(wpcf-status).

From the debug info you can see that it tests the raw value ("accepted"), and so because I am comparing it to the display value ("Accepted"), the test fails.

Instead of using the $(wpc-status) format you can also test with the shortcodes (so you could add the types shortcode with a raw attribute).

I tried to find an example of the format in our documentation, and although the context is slightly different you can see an example in the code block of this section: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/#checking-types-checkboxes-field-for-non-empty-value

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