对于每个项目,添加来自另一个foreach的项目

时间:2015-04-28 08:17:11

标签: php wordpress foreach

我正在使用wordpress,我正在使用一些foreach,这是我的代码:

$content = get_the_content();
if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {
    for ($i = 0; $i < count($match[0]); $i = $i + 1) {
        echo $match[0][$i];
    }
}

$attachments =& get_children($args);
if ($attachments) {
    foreach ($attachments 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; ?>
                    <div class="link-veteran"></div>
                    <div class="popup"><?php echo $imageAlt; ?>
                        <div class="arrow-up"></div>
                    </div>
                    </p>
                    <p><?php echo $match[0][$i]; ?></p>
                </div>
            </div>
            <?php
            break;
        }
    }
} 

我想要第一个附件,第一个描述必须与它一起显示。对于第二个附件,第二个描述必须与它一起显示

1 个答案:

答案 0 :(得分:1)

以下代码是否适合您?

$content = get_the_content();
$j=0;               

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

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

                            echo $match[0][$i];

                        }
                    }       

                    $attachments =& get_children($args);
                        if ($attachments) {
                            foreach($attachments 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; ?><div class="link-veteran"></div><div class="popup"><?php echo $imageAlt; ?><div class="arrow-up"></div></div></p>
                                    <p><?php echo $match[0][$j]; ?></p>
                                </div>
                            </div>

                        <?php
                break;

                    }
                    $j++;
                }
            }