Search code examples
wordpressadvanced-custom-fieldssinglepage

Custom field in pages array


I am creating a singlepage layout where I want to add custom fields from ACF.

<?php $args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'menu_order', //post_title
    'hierarchical' => 1,
    'exclude' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'
);
$pages = get_pages($args);
//start loop
foreach ($pages as $page_data) {
    $content = apply_filters('the_content', $page_data->post_content);
    $title = $page_data->post_title;
    $slug = $page_data->post_name;
?>


<div id='<?php echo "$slug" ?>' class="section">

<div id="inner-content" class="wrap cf">
    <div id="main" class="m-all cf" role="main">
        <div class="content">
            <h2 class="section-title"><?php echo "$title" ?></h2>
                <?php echo "$content" ?>
        </div>             

        <div class="thumb">
            <?php echo get_the_post_thumbnail( $page_data->ID, 'thumb-page' ); ?> 
        </div>
    </div>
</div>


Here's the code. If add the regular code for ACF ( it doesn't show up. (I am having 2 different custom fields, but only one, 'details', need to be included in the loop).

Thank you!


Solution

  • Since you're using a custom foreach loop (rather than a default WP Loop), you need to pass the post ID to the ACF function:

    <?php the_field('details', $page_data->ID) ?>