我的foreach PHP中的问题

时间:2015-04-30 14:20:19

标签: php wordpress foreach

这是我的问题,我必须在这样的图片下显示数组值(描述):

enter image description here

相反,它看起来像这样

enter image description here

我正在解决这个问题,这是我目前的代码:

if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {


                        for( $i = 0; $i < count($match[0]); $i = $i+1 ) {

                            $description[] = $match[0][$i];

                        }

                    }       


                    $attachments =& get_children($args);    
                    $arrayMatches = array();

                        if ($attachments) {
                            foreach(array_chunk($attachments, 2) as $img) {
                                echo '<div class="two_cols">';
                                foreach($img as $attachment) {
                                    foreach($attachment as $attachment_key => $attachment_value) {
                                        $imageID = $attachment->ID;
                                        $imageTitle = $attachment->post_title;
                                        $imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
                                        $imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
                                        $imageURI = $imagearray[0]; // 0 is the URI
                                        $imageWidth = $imagearray[1]; // 1 is the width
                                        $imageHeight = $imagearray[2]; // 2 is the height
                                ?>

                                <div class="col_1_2">
                                    <div id="attachment_<?php echo $imageID; ?>" class="wp-caption alignnone" style="width: 356px;">
                                        <a rel="lightbox-0"  href="<?php echo $imageURI;  ?>"><img class="wp-image-<?php echo $imageID; ?> size-full" title="<?php echo $imageTitle; ?>" src="<?php echo $imageURI; ?>" alt="<?php echo $imageAlt; ?>" width="456" height="304" /></a>
                                        <p class="wp-caption-text"><?php echo $imageTitle; ?></p>
                                        <?php

                                            $arrayMatches[] = $match[0][$j];

                                        ?>
                                    </div>
                                </div>

                            <?php

                            break;

                        }

                        $j++;

                    }

                foreach(array_chunk($arrayMatches, 2) as $desc) {
                        echo '<div class="description">';
                        foreach($desc as $item) {
                            echo $item;
                        }
                        echo '</div>';
                    } 

                echo '</div>';

            }       

        }

我尝试了很多解决方案,但没有一个是好的解决方案。 谢谢你的帮助;)

1 个答案:

答案 0 :(得分:0)

尝试:

foreach(array_chunk($arrayMatches, 2) as $k => $desc) {

    if($k < 1) continue;
    echo '<div class="description">';
    foreach($desc as $item) {
        echo $item;
    }
    echo '</div>';
}