I have a Parent Page “SEO”
Child Pages > “Local SEO”, “Essential SEO”
What i’d like to do is fill out details on the child pages and have them show on the parent page.
I’ve tried a heap of ways and I can’t get the ACF fields from the Child Pages to show up.
Have stripped code back to make it simple:
<section id="service">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- <?php the_content(); ?> -->
<?php $pages = array('child_of' => $post->ID, 'parent' => $post->ID );
$packages = get_pages($pages);
foreach ($packages as $package){
echo '=========' . $package->post_title . '==============';
?>
<?php the_field('title'); ?> // Does Nothing
<?php the_field('title', $post->ID); ?> // Does Nothing
<?php the_field('title', $page->ID); ?> // Gives Notice: Trying to get property of non-object error
<pre>
<?php print_r($package); ?>
<pre>
<?php
}
?>
<?php endwhile; ?>
<?php endif; ?>
</section>
I’ve used print_r($package) and it is correctly picking up the Parent and Child IDs (trimmed for brevity):
=========Essential SEO==============
WP_Post Object
(
[ID] => 186
[post_title] => Essential SEO
[post_parent] => 174
)
=========Local SEO==============
WP_Post Object
(
[ID] => 259
[post_title] => Local SEO
[post_parent] => 174
)
I’ve tried 3 options to get the fields:
<?php the_field(‘title’); ?> // Does Nothing
<?php the_field(‘title’, $post->ID); ?> // Does Nothing
<?php the_field(‘title’, $page->ID); ?> // Gives Notice: Trying to get property of non-object error
Obviously I’m missing how to get access to the Child pages – but I can’t work out how.
Be very grateful for any advice
Cheers
webecho
Solved!
I needed to use $package->ID … of course.
Can I delete the Post? – was just a stupid error
but not a unique error. I was going to point out what you found. The entire thing can probably be deleted if you really want but it may help someone else down the road, it also show how to get fields from child posts which could also be useful to someone. All in all a good topic.
The topic ‘Show Child Fields on Parent page’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.