I am trying to: Display a field from the post parent if the post itself does have a value.
I have a custom field for "phone" to show a phone number and I need some help on how to put this conditional statement together.
I want to see if the "phone" field is not empty on the post and if it has a value to output it. But if it is empty then I want to see if the post parent has a value in a field of the same name (phone) and then display that.
My post parent is "clinics"
Thanks
Hello Farrel.
Thank you for contacting the Toolset Support.
In order to display a value of a custom field of the parent post to the View or Content Tempalte of the child post, what you need to do is use a code like below:
[types field="your-field-name" id="$parent-post-type-slug"][/types]
So for your case, you could do something like
Department Name: [wpv-post-title]<br />
Phone number:
[wpv-conditional if="( $(wpcf-phone-number) ne '' )"]
[types field='phone-number'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-phone-number) eq '' )"]
[types field="phone-number" id="$clinic"][/types]
[/wpv-conditional]
Where:
“clinic” is the custom post type slug for the parent post
“phone-number” is the custom post field slug regarding the phone number for both “Clinics” and “Departments”
“Departments” is the child custom post type that has “Clinics” as parent custom post type
In case you also want to check if the “phone-number” field value exists in the parent by using the code below
[wpv-conditional if="( '[types field="phone-number" id="$clinic"][/types]' ne '' )"]
Things to do if the "phone-number" field value exists in the parent ("Clinics")
[/wpv-conditional]
If you have any further issues, please let me know.
Regards
Great, thanks! I just have a question. I assume 'ne' means not empty. Is that correct?
And what does eq ' ' mean? Retrieve everything if you don't specify a value to be equal to?
Hello Farrel,
'ne' means "not equal".
In your case " eq '' " means "if the custom post field 'wpcf-phone-number' is not set or equals with an empty string'.
If you have any further issues, please let me know.
Regards