Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I use the https://github.com/humanmade/Custom-Meta-Boxes/ to make this custom repeatable custom fields to wordpress. The given arrays are like this

    Array
(
    [0] => Array
        (
            [photoset-caption] => Array
                (
                    [cmb-field-0] => Caption1
                    [cmb-field-1] => Caption2
                    [cmb-field-2] => Caption3
                    [cmb-field-3] => Caption4
                )

            [photoset-image] => Array
                (
                    [cmb-field-0] => 17
                    [cmb-field-1] => 16
                    [cmb-field-2] => 15
                    [cmb-field-3] => 14
                )

        )

    [1] => Array
        (
            [photoset-caption] => Array
                (
                    [cmb-field-0] => Caption1
                    [cmb-field-1] => Caption2
                    [cmb-field-2] => Caption3
                    [cmb-field-3] => Caption4
                )

            [photoset-image] => Array
                (
                    [cmb-field-0] => 17
                    [cmb-field-1] => 16
                    [cmb-field-2] => 15
                    [cmb-field-3] => 14
                )

        )

)

The loop it try to make is this.

 // get the custom fields for this post
    $photoset = get_post_meta($post->ID, 'photoset_group_fields', false ); 

        echo '<div class="photoset">';
        echo '<div class="photoset-row">';

        foreach($photoset as $photosetloop){
        echo '<figure class="photoset-item">';
        echo '<div>' . wp_get_attachment_image($photosetloop['photoset-image'], 'large' ) . '</div>';
        echo '<figcaption>' . $photosetloop['photoset-caption'] .'</figcaption>';
        echo '</figure>';
        }   

        echo '</div>';
        echo '</div>';

So the loop haves .photoset-item and inside it have image and caption. My question how to I foreach it, thanks.

I did update for the array, i have group that I loop.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

The simplest way it's to make two foreachs, you can do it recursive with a function if you want as well.

<?php
$photos =array(
    'photoset-caption' => array(
            'cmb-field-0' => 'Caption1',
            'cmb-field-1' => 'Caption2',
            'cmb-field-2' => 'Caption3',
            'cmb-field-3' => 'Caption4'
     ),
    'photoset-image' => array(
            'cmb-field-0' => 17,
            'cmb-field-1' => 16,
            'cmb-field-2' => 15,
            'cmb-field-3' => 14
    )
);

    foreach($photos as $k => $photo){

        echo '<h1>'.$k.'</h1>';

        foreach($photo as $key => $value){
            echo $key.': '. $value.'<br/>';
        }
        echo '<hr/>';
    }

Check an example here: example

share|improve this answer

Not the most elegant way to do it, but it does work in this case.

$arr = Array
(
    "photoset-caption" => Array
    (
        "cmb-field-0" => "Caption1",
        "cmb-field-1" => "Caption2",
        "cmb-field-2" => "Caption3",
        "cmb-field-3" => "Caption4"
    ),

"photoset-image" => Array
    (
        "cmb-field-0" => 17,
        "cmb-field-1" => 16,
        "cmb-field-2" => 15,
        "cmb-field-3" => 14
    )
);

$count = count($arr["photoset-caption"]);
for($i = 0; $i < $count; ++$i) {
    echo 'Loop #'.$i.'<br>';
    echo $arr["photoset-caption"]["cmb-field-".$i], '<br>';
    echo $arr["photoset-image"]["cmb-field-".$i], '<br>';
    echo '<br>';
}

You can paste it here: http://writecodeonline.com/php/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.