最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Accessing values from ACF sub field (flexible content area) in PHP

programmeradmin4浏览0评论

I'm trying to access the values of a ACF sub field using PHP.

$articles = get_posts(
 array(
  'posts_per_page' => -1,
  'post_status' => 'publish',
  'post_type' => array('venues', 'suppliers'),
 )
);


$locations = array();
foreach ($articles as $article) {

    $field = get_field('info_items', $article->ID, false);

    if( have_rows('info_items') ):
        while( have_rows('parent_field') ): the_row();

        $value = get_sub_field('info_location');

        endwhile;

    endif;
    var_dump($value);
    exit();
}

In the code above if I var_dump the contents of $field I get the array showing all the subfields - including the one I want:

array(3) { [0]=> array(2) { ["acf_fc_layout"]=> string(13) "info_location" ["field_59ea62cf04adf"]=> string(16) "Manchester" } [1]=> array(2) { ["acf_fc_layout"]=> string(10) "info_phone" ["field_59ea634904ae3"]=> string(11) "08009788221" } [2]=> array(3) { ["acf_fc_layout"]=> string(12) "info_website" ["field_59ea635c04ae5"]=> string(32) "" ["field_5c6802013bb34"]=> string(0) "" } } NULL

I'm trying to retrieve the 'info_location' value - in this example the string 'Manchester'.

The have_rows loop is from the ACF website docs for get_sub_field but doesn't work in this context.

I'm trying to access the values of a ACF sub field using PHP.

$articles = get_posts(
 array(
  'posts_per_page' => -1,
  'post_status' => 'publish',
  'post_type' => array('venues', 'suppliers'),
 )
);


$locations = array();
foreach ($articles as $article) {

    $field = get_field('info_items', $article->ID, false);

    if( have_rows('info_items') ):
        while( have_rows('parent_field') ): the_row();

        $value = get_sub_field('info_location');

        endwhile;

    endif;
    var_dump($value);
    exit();
}

In the code above if I var_dump the contents of $field I get the array showing all the subfields - including the one I want:

array(3) { [0]=> array(2) { ["acf_fc_layout"]=> string(13) "info_location" ["field_59ea62cf04adf"]=> string(16) "Manchester" } [1]=> array(2) { ["acf_fc_layout"]=> string(10) "info_phone" ["field_59ea634904ae3"]=> string(11) "08009788221" } [2]=> array(3) { ["acf_fc_layout"]=> string(12) "info_website" ["field_59ea635c04ae5"]=> string(32) "https://www.stuffandstuff.co.uk" ["field_5c6802013bb34"]=> string(0) "" } } NULL

I'm trying to retrieve the 'info_location' value - in this example the string 'Manchester'.

The have_rows loop is from the ACF website docs for get_sub_field but doesn't work in this context.

Share Improve this question asked Dec 30, 2019 at 13:25 stevesteve 1176 bronze badges 1
  • 1 Check while loop in your code. You need to pass parent field there. – Vimal Usadadiya Commented Dec 30, 2019 at 13:30
Add a comment  | 

1 Answer 1

Reset to default 1

You iterate through posts obtained by the get_posts(), so you have to pass post ID as the second parameter of the function have_rows(). By default ID of current post is used.

foreach ($articles as $article) {

    $value = null;
    if( have_rows( 'info_items', $article->ID ) ): 
        while( have_rows( 'info_items', $article->ID ) ): the_row();

            $value = get_sub_field('info_location');

        endwhile;

    endif;

    echo "<br>{$article->ID} ";
    var_dump($value);
}
发布评论

评论列表(0)

  1. 暂无评论