Skip Navigation

[Resolved] No luck displaying child posts using custom template

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

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

Assisted by: Luo Yang.

Author
Posts
#1312979

Hello
I have a parent called [Department] slug `department and a child called [imagedates] slug `imagedate.
Established a relationship one to many, department to many imagedates.
My custom fields are managed by ACF pro so initial plan was to use Toolset Views to display them which worked fine to display just text fields loading non Toolset fields, but it dosent work to display image fields.
So now what im trying to do is display child posts with the code below:

Tried this code which it should work, but nothing shows up

<?php
$args = array( 'post_parent' => get_the_ID(), 'post_type' => 'department', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>

<h2><?php the_title(); ?></h2>
<a href="<?php the_permalink(); ?>">
    <p><?php     the_field('image_text'); ?></p>
</a>

<?php endwhile;  wp_reset_postdata(); ?>
 

But nothing shows up

tried this also:

<?php
    $children = get_children( array('post_parent' => get_the_ID()) );
    foreach ( $children as $children_id => $child ) {
        echo $child->post_title;
        echo str_replace( ']]>', ']]>',apply_filters( 'the_content', $child->post_content )); 
    }
?>

but not luck.
Any advice or insight idea whats wrong with the code would be great. If can still show image fields from ACF with just a View i can go that way also.

Thank you

#1313109

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should use the Post Relationship API function toolset_get_related_posts in this case to retrieve all related posts.
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For example:

global $post;
$related_posts = toolset_get_related_posts(
                                    $post->ID,    /// adjust if needed
                                    'relationship-slug',   /// adjust if needed
                                     'parent',
                                     1000,
                                      0,
                                      array(),
                                     'post_object',
                                     'child'
                                     );

foreach ($related_posts as $related_post) {
   // add whatever you want
}

You can find the relationship slug from: Toolset => Relationships => Edit your relationship and copy the slug.

#1313195

Thank you for your code example.
It works but it only shows the count of child posts. For example Parent post is k1 so it shows child posts k1 k1 k1 which are three child posts.
Is there a way to display child post fields inside the loop, custom fields of toolset?
If you have any examples that would be great.
Thank you

#1314213

Hello,

In the codes mentioned above:

foreach ($related_posts as $related_post) {
   // add whatever you want
}

You can get the post ID insider above loop, like this:
$post_id = $related_post->ID;

And pass $post_id as parameter to Types function types_render_field(), for example:
types_render_field( "my-field-slug", array("post_id"=>$post_id ) );

See our document:
https://toolset.com/forums/topic/how-to-render-custom-fields-using-types/