[Resolved] How to display the child value in a parent (list) view?
This thread is resolved. Here is a description of the problem and solution.
Problem:
How to display conditional data inside views based on child post custom field values
Solution:
We can write custom shortcode to check if a particular post has child post and if that has child post we will get custom field values of child post using "get_post_meta()" function.
For example, What you can do is to create a Custom ShortCode where you perform a check if parent post has child post.
You can also use the Types API to build this query, such a ShortCode can look like this:
function child_exists_func( $atts ){
$child_posts = types_child_posts('your-child-post-type-slug-here');
if ($child_posts) {
return true;
}
}
add_shortcode( 'child-exists', 'child_exists_func' );
Where:
Replace 'your-child-post-type-slug-here' with your child post slug.
This above ShortCode would be used on the PARENT post type and returns true if a Child exists.
Of course, you can extend this to return the ID, or whatever you want of the Child Post
Also, you should include a case for if no child exists, as example you can return false.
I already did this and this is working:
"For example, What you can do is to create a Custom ShortCode where you perform a check if parent post has child post."
What do you mean by "Of course, you can extend this to return the ID, or whatever you want of the Child Post"
=> I mean to say currently we return 'true' or 'false' based on the condition that if post has child or not. You can return any value based on your requirement.
I read the documentation, but I don't see a sample of something like
[wpv-conditional if="( '[has_children_of_type post_type="testimonial" ]' eq '1' )"]
display the child-value of the child-field-option
[/wpv-conditional]
I know wpv-conditional is given true and then I want to display a child-value.
is it something like
[types child-field="option" output="raw"][/types]
?
i'm not a php coder , so I'm looking for help to make my code working.
Could you please share problem URL as well as which view you are using?
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Could you please let me know the complete range of this condition:
IF there is a child (AANMELDING)
display the field option (value)
if the child-field option = 5104 then display "zaterdag"
if the child-field option = 5106 then display "zondag"
.etc
.etc
.etc
if the child-field option =7625 then display "zaterdag en zondag"
END
means how many combinations you have lets say "child-field option = 5104 then display "zaterdag""
then if child field option = xx then display yyyy.
if the child-field option = 5104 then display "zaterdag 30 april"
if the child-field option = 5106 then display "zondag 1 mei"
if the child-field option = 5107 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 5144 then display "zaterdag 30 april"
if the child-field option = 5145 then display "zondag 1 mei"
if the child-field option = 5146 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 5147 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 7622 then display "zaterdag 30 april"
if the child-field option = 7623 then display "zondag 1 mei"
if the child-field option = 7624 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 7625 then display "zaterdag 30 april en zondag 1 mei"
The problem now is: every ad is showing the standard custom text (blue "highlighted") it is only needed when there is a child. Otherwise it is wrong to show the standard custom text.
I'm missing now the wpv-conditional in the view?
[wpv-conditional if="( '[has_children_of_type post_type="aanmelding" ]' eq '1' )"]
<div class="blue" style="margin-top: 9px">standard custom text (plus the option description)</div>
[/wpv-conditional]
I mean with "(plus the option description)" the value of the array.