我正在编辑一个Wordpress插件,如果我为多个图像设置滑块,它会显示一个选项,它会显示帖子列表中的所有图像,但我只需要先显示。如何在第一次循环后停止执行?
有代码
<?php if( isset( $postMetaData['_otw_slider_image'] ) && $postMetaData['_otw_slider_image'] ){?>
<?php $imagePath = parse_url( $sliderImages[0] );?>
<?php if( isset( $sliderImages[0] ) ){?>
<?php if( isset( $this->listOptions['image_link'] ) && in_array( $this->listOptions['image_link'], array( 'lightbox' ) ) ){?>
<?php $first_shown = false; ?>
<?php foreach( $sliderImages as $sImage ){?>
<?php $imagePath = parse_url( $sImage );?>
<a href="<?php echo $this->otwImageCrop->resize( $imagePath['path'], $imageLightboxWidth, $imageLightboxHeight, $this->imageCrop, $this->imageWhiteSpaces, $this->imageBackground, $imageLightboxFormat )?>" rel="otw_fslide_<?php echo $post->ID?>" title="<?php echo htmlentities( $post->post_title );?>" class="otw_in_slider otw_in_slider_<?php echo intval( $first_shown )?>" >
<?php if( !$first_shown ){?>
<img src="<?php echo $this->otwImageCrop->resize( $imagePath['path'], $this->imageWidth, $this->imageHeight, $this->imageCrop, $this->imageWhiteSpaces, $this->imageBackground, $this->imageFormat )?>" alt="" />
<?php } ?>
</a>
<?php $first_shown = true;?>
<?php }?>
<?php }elseif( isset( $this->listOptions['image_link'] ) && in_array( $this->listOptions['image_link'], array( 'single' ) ) ){?>
<a href="<?php echo $permaLink?>">
<img src="<?php echo $this->otwImageCrop->resize( $imagePath['path'], $this->imageWidth, $this->imageHeight, $this->imageCrop, $this->imageWhiteSpaces, $this->imageBackground, $this->imageFormat )?>" alt="" />
</a>
<?php }else{ ?>
<img src="<?php echo $this->otwImageCrop->resize( $imagePath['path'], $this->imageWidth, $this->imageHeight, $this->imageCrop, $this->imageWhiteSpaces, $this->imageBackground, $this->imageFormat )?>" alt="" />
<?php }?>
<?php }?>
<?php }else{?>
<div class="otw_portfolio_manager-slider" data-animation="slide">
<ul class="slides otw_portfolio_ul_slider">
<?php foreach( $sliderImages as $sliderImage ){?>
<li>
<?php
$imagePath = parse_url($sliderImage);
$sliderImgLink = false;
?>
<?php if( isset( $this->listOptions['image_link'] ) && in_array( $this->listOptions['image_link'], array( 'lightbox' ) ) ){ $sliderImgLink = true;?>
<a href="<?php echo $sliderImage?>" rel="otw_fslide_<?php echo $post->ID?>" class="otw_portfolio_manager-fancybox-slider" title="<?php echo htmlentities( $post->post_title );?>"></a>
<?php }?>
<?php if( isset( $this->listOptions['image_link'] ) && in_array( $this->listOptions['image_link'], array( 'single' ) ) ){ $sliderImgLink = true;?>
<a href="<?php echo $imgLink?>" title="<?php echo $post->post_title;?>">
<?php }?>
<img src="<?php echo $this->otwImageCrop->resize( $imagePath['path'], $this->imageWidth, $this->imageHeight, $this->imageCrop, $this->imageWhiteSpaces, $this->imageBackground, $this->imageFormat )?>" alt="" data-item="media">
<?php if( $sliderImgLink ){?>
</a>
<?php }?>
</li>
<?php }?>
</ul>
</div>
<?php }?>
答案 0 :(得分:2)
这是一个可以在代码中执行相同操作的示例。
$i = 0;
foreach ($largedata as $data) {
if($i == 0) {
echo $data->id;
}
$i++;
}
希望你能从例子中得到它。
答案 1 :(得分:0)
不是不必要地循环遍历整个$sliderImages
数组(假设这里是一个数组,而不是Iterator
),只需从数组中抓取第一个滑块图像:
<?php if (isset( $this->listOptions['image_link'] ) && in_array($this->listOptions['image_link'], array('lightbox' ))) { ?>
<?php $sliderImages = array_values($sliderImages); ?>
<?php $sImage = $sliderImages[0]); ?>
<?php $imagePath = parse_url($sImage); ?>
<a href="<?php echo $this->otwImageCrop->resize( $imagePath['path'], $imageLightboxWidth, $imageLightboxHeight, $this->imageCrop, $this->imageWhiteSpaces, $this->imageBackground, $imageLightboxFormat )?>" rel="otw_fslide_<?php echo $post->ID?>" title="<?php echo htmlentities( $post->post_title );?>" class="otw_in_slider otw_in_slider_<?php echo intval( $first_shown )?>">
<img src="<?php echo $this->otwImageCrop->resize( $imagePath['path'], $this->imageWidth, $this->imageHeight, $this->imageCrop, $this->imageWhiteSpaces, $this->imageBackground, $this->imageFormat )?>" alt="" />
</a>
<?php } elseif (isset($this->listOptions['image_link'] ) && in_array($this->listOptions['image_link'], array( 'single' ))) { ?>
<?php // other logic here?>
<?php } ?>
注意使用array_values()
规范化$sliderImages
的键,因此可以使用0
作为键来引用它的第一个元素(不知道是什么$sliderImages
的键看起来像)。
供参考,见: