Support

Account

Home Forums General Issues How can I display the values of a repeater-field from a post object?

Solving

How can I display the values of a repeater-field from a post object?

  • I wanted to display a repeater field from another post via Post object (setting: Post ID).

    var_dump shows all values – but how do I bring them in a for-each loop? The problem is that it is quite a nested array

    
    $materialien_repeater = get_field('materialien', $materialien_post);
    
    array(1) { ["materials"]=> array(4) { [0]=> array(1) { ["link-composition"]=> array(3) { ["img-url"]=> string(73) "https://cdn.pixabay.com/photo/2018/11/16/13/57/cosmos-3819483_960_720.jpg" ["link-url"]=> string(72) "https://pixabay.com/de/photos/kosmos-himmel-universum-raum-star-3819483/" ["copyright"]=> string(21) "@alinkon @pixabay.com" } } [1]=> array(1) { ["link-composition"]=> array(3) { ["img-url"]=> string(71) "https://cdn.pixabay.com/photo/2012/01/09/10/31/planet-11613_960_720.jpg" ["link-url"]=> string(71) "https://pixabay.com/de/photos/planet-mars-krater-victoria-krater-11613/" ["copyright"]=> string(20) "@WikiImages @pixabay" } } [2]=> array(1) { ["link-composition"]=> array(3) { ["img-url"]=> string(72) "https://cdn.pixabay.com/photo/2016/08/24/14/29/earth-1617121_960_720.jpg" ["link-url"]=> string(79) "https://pixabay.com/de/illustrations/erde-planet-welt-globus-weltkarte-1617121/" ["copyright"]=> string(20) "@PIRO4D @pixabay.com" } } [3]=> array(1) { ["link-composition"]=> array(3) { ["img-url"]=> string(109) "https://images.pexels.com/photos/757158/pexels-photo-757158.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" ["link-url"]=> string(74) "https://www.pexels.com/de-de/foto/
    
  • Instead of getting the repeater value you loop over it the normal way

    
    if (have_rows('materialien', $materialien_post)) {
      while (have_rows('materialien', $materialien_post)) {
        the_row()
        // .... etc
      }
    }
    
  • pls check foreach ($materialien_repeater[‘materials’] as $material) {
    $imgUrl = $material[‘link-composition’][‘img-url’];
    $linkUrl = $material[‘link-composition’][‘link-url’];
    $copyright = $material[‘link-composition’][‘copyright’];

    // Output or process values as needed
    echo “Image: $imgUrl, Link: $linkUrl, Copyright: $copyright<br>”;
    }

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.