PHP-在显示的第一个图像处停止foreach
我正在编辑一个Wordpress插件,如果我为多个图像设置了滑动条,它会显示帖子列表上的所有图像,但我只需要第一个。如何在第一次循环后停止执行 这是密码PHP-在显示的第一个图像处停止foreach,php,wordpress,Php,Wordpress,我正在编辑一个Wordpress插件,如果我为多个图像设置了滑动条,它会显示帖子列表上的所有图像,但我只需要第一个。如何在第一次循环后停止执行 这是密码 <?php if( isset( $postMetaData['_otw_slider_image'] ) && $postMetaData['_otw_slider_image'] ){?> <?php $imagePath = parse_url( $sliderIm
<?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 }?>
otwImageCrop->resize($imagePath['path'],$this->imageWidth,$this->imageHeight,$this->imageCrop,$this->imageWhiteSpaces,$this->imageBackground,$this->imageFormat)?>“alt=”“/>
-
这是一个您可以在代码中执行相同操作的示例
$i = 0;
foreach ($largedata as $data) {
if($i == 0) {
echo $data->id;
}
$i++;
}
希望您能从示例中获得它。这是一个您可以在代码中执行相同操作的示例
$i = 0;
foreach ($largedata as $data) {
if($i == 0) {
echo $data->id;
}
$i++;
}
希望您能从示例中获得它。不要在整个
$sliderImages
数组中不必要地循环(假设它是一个数组,而不是迭代器),只需从数组中获取第一个滑块图像:
<?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 } ?>
不要不必要地在整个$sliderImages
数组中循环(假设它是一个数组,而不是迭代器),只需从数组中获取第一个滑块图像:
<?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 } ?>
你可以用break;
太多来停止foreach循环,如果你只需要第一个结果,为什么还要循环?这是我见过的最混乱的代码。@Andreas那不是我的插件:)就像我说的,我正在编辑这个你可以用break;
太多来停止foreach循环如果你只需要第一个结果,你为什么要ooping?这是我见过的最混乱的代码。@Andreas那不是我的插件:)就像我说的,我正在编辑它。如果你只想处理第一个图像,你不想遍历所有内容。如果你只想处理第一个图像,你不想遍历所有内容。