How to get Child Pages of Parent in WordPress with Advanced Custom Fields
In a recent white label website development, the client of a housing development company required a bespoke house listing and we love using ACF Pro for all our projects as we think it’s user-friendly for the customer in the back end.
If you use ACF then getting them to show on the front end can be tricky, especially if you put them in a group or repeater field, below is the code we used to get the child page house type listings for the housing development which is a parent page.
Get the ‘house_details’ which is the ACF group within the WordPress admin panel
$housedet = get_field(‘house_details’, $page->ID);?>
If you have any questions please comment below or drop us an email for any help of advice.
<?php $ancestor_id = $post->$page_id = get_queried_object_id(); $descendants = get_pages(array('child_of' => $ancestor_id)); $incl = ""; foreach ($descendants as $page) { if (($page->post_parent == $ancestor_id) || ($page->post_parent == $post->post_parent) || ($page->post_parent == $post->ID)){ $incl .= $page->ID . ",";}}?> <?php $mypages = get_pages( array( 'child_of' => $ancestor_id, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) ); foreach( $mypages as $page ) {$content = $page->post_content; if ( ! $content ) // Check for empty page continue; $content = apply_filters( 'the_content', $content ); $housedet = get_field('house_details', $page->ID);?> <div class="col span_1_of_3 feathome"><a href="<?php echo get_page_link( $page->ID ); ?>"> <?php echo get_the_post_thumbnail( $page->ID, 'large' ); ?> <h2 class="txtgrey"><?php echo $page->post_title; ?></h2> <p class="gofull txtgrey"><?php echo $housedet['house_type_description']; ?></p> <p class="gofull">Prices from <?php echo get_field( 'house_type_prices_from', $page->ID ); ?></p> <p class="gofiftylf txtteal"><i class="far fa-bed"></i> <?php echo $housedet['bedrooms']; ?></p> <p class="gofiftylf txtteal"><i class="far fa-bath"></i> <?php echo $housedet['bathrooms']; ?></p> <p class="gofiftylf txtteal"><i class="far fa-home"></i> <?php echo get_field( 'square_ft', $page->ID ); ?> sq.ft.</p> <p class="gofull txtpurple" style="text-decoration: underline">View this home</p> </a> </div> <?php } ?> </div> </div>