0

Complete newbie to PHP but not bad at cobbling together. I've started using Advanced Custom Fields for Wordpress which helps immensely.

I've got the code together that turns a gallery field into a jQuery slider (you chuck in the images in the back end and it loops through and creates the slider). However, what I'd like it to do is only load a static image if there is only one image entered. In psudo-code:

if field contains more than one image, loop through the slider code else (if there is only one image) just wrap the one image in img tags.

Current live code that generates the slider:

<?php

/* Create the Markup for a slider */

$images = get_field('hero_image_gallery');

if( $images ): ?>
    <div id="slider" class="flexslider">
        <ul class="slides">
            <?php foreach( $images as $image ): ?>
                <li>
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
                    <p class="flex-caption"><?php echo $image['caption']; ?></p>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; 

?>

Any ideas? Initially I thought I'd want to count $images but I'm not sure that contains the number of images in the gallery.

Edit: Just dumped out the hero_image_gallery which starts:

array(2)

There's currently 2 images in there and if I add a third, I get array(3).

Edit 2: Full dump of hero_image_gallery below. I've had to edit out some details as I can only post 1 URL as a newbie.

array(2) { [0]=> array(9) { ["id"]=> int(971) ["alt"]=> string(14) "School" ["title"]=> string(12) "home-image-2" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["url"]=> string(74) "#" ["width"]=> int(1260) ["height"]=> int(860) ["sizes"]=> array(9) { ["thumbnail"]=> string(74) "#" ["thumbnail-width"]=> int(150) ["thumbnail-height"]=> int(102) ["medium"]=> string(74) "#" ["medium-width"]=> int(300) ["medium-height"]=> int(204) ["large"]=> string(74) "#" ["large-width"]=> int(1024) ["large-height"]=> int(698) } } [1]=> array(9) { ["id"]=> int(977) ["alt"]=> string(0) "" ["title"]=> string(12) "home-image-1" ["caption"]=> string(0) "" ["description"]=> string(0) "" ["url"]=> string(74) "#" ["width"]=> int(1260) ["height"]=> int(860) ["sizes"]=> array(9) { ["thumbnail"]=> string(74) "#" ["thumbnail-width"]=> int(150) ["thumbnail-height"]=> int(102) ["medium"]=> string(74) "#" ["medium-width"]=> int(300) ["medium-height"]=> int(204) ["large"]=> string(74) "#" ["large-width"]=> int(1024) ["large-height"]=> int(698) } } }

4

1 回答 1

0

我想你可能已经回答了你自己的问题!如果您当前有 2 个图像,并且当您查看返回的数组并返回 2 个结果时,那么要检查您是否只有 1 个图像,您可以执行以下操作:

if( count($images) == 1 )

编辑:添加完整代码

<?php

/* Create the Markup for a slider */

$images = get_field('hero_image_gallery');

if( $images && count($images) == 1 ) { ?>
   <!-- there is only 1 image -->
<?php } else if ( $images && count($images) > 1 ) { ?>
    <!-- there is more than 1 image -->
    <div id="slider" class="flexslider">
        <ul class="slides">
            <?php foreach( $images as $image ): ?>
                <li>
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
                    <p class="flex-caption"><?php echo $image['caption']; ?></p>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php } ?>
于 2013-07-23T13:50:47.390 回答