Question

I'm using advanced custom fields and Im trying to pull content from sub pages onto the homepage

I cant seem to get the below to work, does anyone have any why its not working?

The page setup is like:

home

-parent page (page id = 4)

--sub page

--sub page

<ul>
    <?php
    $children = get_children( array( 'post_parent' => get_the_ID(4) ) );
    if ( $children ) {
        foreach( $children as $child ) { ?>
            <li>
                <?php echo get_the_title($child->ID); ?>
                <?php the_field( 'content', $child->ID ); ?>
            </li>
    <?php } } ?>
</ul>
Was it helpful?

Solution

The "get_the_ID()" function is used incorrectly, and can be left out. Your code becomes:

$children = get_children( array( 'post_parent' => 4 ) );

You might also want to specify the post_type parameter. Now your function will also return attachments.

$children = get_children( array( 'post_parent' => 4, 'post_type' => 'page' ) );

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top