0

foreachループで以下の構造を使用して結果を達成しようとしています。このループでは、2つの画像ごとに構造全体が繰り返されます。

使用できるものについての基本的な知識があります。カウンター++; %2よりも大きいですが、構文とコードでの使用方法がわかりません。

<?php
    function dt_attached($postid=0, $size='thumbnail', $attributes='', $linksize='full', $count=-1) {
        if ($postid<1) $postid = get_the_ID();
        if ($images = get_children(array(
            'post_parent' => $postid,
            'post_type' => 'attachment',
            'numberposts' => $count,
            'post_mime_type' => 'image',)))

            foreach($images as $image) {
                $attachment=wp_get_attachment_image_src($image->ID, 'thumbnail');
                $small_image = wp_get_attachment_image_src($image->ID, 'midium');
                $big_image = wp_get_attachment_image_src($image->ID, 'full');
                ?>

                <div class="mainrow">

                    <div class="block">
                        <a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
                            <img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
                        </a>
                    </div>

                    <!--[I want to get two images in mainrow]-->
                    <div class="block">
                        <a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
                            <img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
                        </a>
                    </div>

                </div>

                <?php //the_attachment_link($image->ID, false, true, false); ?>
        <?php }
    }
?>

したがって、3つ以上の画像がある場合は、html構造全体が繰り返されます。あなたの助けをどうもありがとう

4

1 に答える 1

2

あなたのコメントから私が集めたのは、行ごとに 2 つの画像を表示し、余分な画像が 1 つある場合は、最終行のその横にプレースホルダーを表示したいということです。

必要なのは、画像の数と、偶数か奇数かを数えることだけです。次に、(インクリメント カウンターを使用して) 最後の画像にいることがわかったら、プレースホルダーを追加します。

あなたのコードが行っていないのは、2 つの画像を 1 行に配置することです。%そのためには、カウンターのモジュロ ( ) も取得する必要があります。

<?php
$counter = 0;
$imgCount = count($images);

foreach ($images as $image) {
    $attachment  = wp_get_attachment_image_src($image->ID, 'thumbnail');
    $small_image = wp_get_attachment_image_src($image->ID, 'midium');
    $big_image   = wp_get_attachment_image_src($image->ID, 'full');

    if ($counter % 2 == 0): ?>
    <div class="mainrow">
    <?php endif; ?>

        <div class="block">
            <a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
                <img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
            </a>
        </div>

    <?php if ($counter++ % 2 == 1): ?>
    </div>
    <?php endif; ?>

    <?php //the_attachment_link($image->ID, false, true, false); ?>
<?php
}

// Since (if there are an odd number of images) the loop may not close the <div>, 
// we have to make sure it does.
if ($counter % 2 == 0) {
    ?>
    <!-- placeholder goes here -->
    </div>
<?php
}
于 2012-08-14T13:09:33.830 に答える