Holt DevOps News

Get Child Pages of Parent in WordPress with Advanced Custom Fields

News Categories: Wordpress

This article was published on: 03/11/20 4:49 PM

Get Child Pages of Parent in WordPress with Advanced Custom Fields

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>

Let's build a better web together

Get in touch with us for a quick quote

Web Development Agency, We Website Design

We’re eCommerce Experts in Cutting-edge Design & Development Technologies - Worldwide accreditation from leading digital platform providers including Shopify, WordPress & Magento.

Web Development Agency Manchester

Woolden Road
Manchester
M44 5JX


Web Development Agency London

71-75 Shelton Street
Covent Garden
London
WC2H 9JQ


Let's hook up

hello@wewebsitedesign.io

0161 676 8989

Stalk Us On Social

What We Do

  • Shopify
  • WordPress
  • Magento
  • SEO
  • Aero Commerce
  • Shopify 2.0

Copyright 2022 We Agency - White Label Web Development Digital Agency.
Registered in England & Wales We Agency is a trading division of We Website Design Ltd. Company Registration Number 14175442 Privacy Policy

Ready to work with us?

Drop us your details and we will arrange a social distanced meet & greet or video chat so you can see our beautiful faces.

    Name*

    Email*

    Telephone