Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP只运行一次foreach循环_Php - Fatal编程技术网

使用PHP只运行一次foreach循环

使用PHP只运行一次foreach循环,php,Php,在我的WordPres中,我有以下PHP循环浏览图像 function marty_get_images($post_id) { global $post; $thumbnail_ID = get_post_thumbnail_id(); $images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post

在我的WordPres中,我有以下PHP循环浏览图像

function marty_get_images($post_id) {
global $post;

$thumbnail_ID = get_post_thumbnail_id();

$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

if ($images) :

    foreach ($images as $attachment_id => $image) :

    if ( $image->ID != $thumbnail_ID ) :

        $img_title = $image->post_title;   // title.
        $img_caption = $image->post_excerpt; // caption.
        $img_description = $image->post_content; // description.

        $img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
        if ($img_alt == '') : $img_alt = $img_title; endif;

        $big_array = image_downsize( $image->ID, 'large' );
        $big_img_url = $big_array[0];

        $thumb_array = image_downsize( $image->ID, 'thumbnail' );
        $thumb_img_url = $thumb_array[0];

        ?>

        <a href="<?php echo $big_img_url; ?>" class="thumb"><img src="<?php echo $thumb_img_url; ?>" alt="<?php echo $img_alt; ?>" title="<?php echo $img_title; ?>" /></a>

    <?php endif; ?>
    <?php endforeach; ?>

<?php endif;
函数marty_获取图像($post_id){
全球$员额;
$thumbnail\u ID=get\u post\u thumbnail\u ID();
$images=get_子项(数组('post_parent'=>$post_id','post_status'=>'inherit','post_type'=>'attachment','post_mime_type'=>'image','order'=>'ASC','orderby'=>'menu_order id');
如果($images):
foreach($attachment\u id=>$image形式的图像):
如果($image->ID!=$thumbnail\u ID):
$img_title=$image->post_title;//title。
$img_caption=$image->post_摘录;//标题。
$img\u description=$image->post\u content;//说明。
$img_alt=get_post_meta($attachment_id,'.'wp_attachment_image_alt',true);//alt
如果($img_alt=''):$img_alt=$img_title;endif;
$big_array=image_-downsize($image->ID,'large');
$big_img_url=$big_数组[0];
$thumb\u array=image\u-downsize($image->ID,'thumbnail');
$thumb_img_url=$thumb_数组[0];
?>
您可以使用以下语句:

<?php endif; ?>
<?php break; ?>
<?php endforeach; ?>

您可以使用以下语句:

<?php endif; ?>
<?php break; ?>
<?php endforeach; ?>

在HTML链接后添加
break;
。这将中断
foreach
循环,并继续执行循环后的任何其他代码


在HTML链接后添加
break;
。这将中断
foreach
循环,并继续执行循环后的任何其他代码


对于只有一次迭代,您可以对查询使用“LIMIT”,该查询将只从数据库返回一条记录

对于条件匹配,只进行一次迭代

if ( $image->ID != $thumbnail_ID ) :
您可以添加break;before

 Break;
<?php endif; ?>
<?php endforeach; ?>
Break;
这将检查缩略图的一个条件匹配

对于只有一次迭代,您可以对查询使用“LIMIT”,该查询将只从数据库返回一条记录

对于条件匹配,只进行一次迭代

if ( $image->ID != $thumbnail_ID ) :
您可以添加break;before

 Break;
<?php endif; ?>
<?php endforeach; ?>
Break;
这将检查缩略图的一个条件匹配


最好使用array_slice并运行一次循环

   foreach (array_slice($images,0,1 )as $attachment_id => $image) {
}
这里,, 0=循环开始的索引形式,
1=运行一次循环

最好使用array\u slice并运行一次循环

   foreach (array_slice($images,0,1 )as $attachment_id => $image) {
}
这里,, 0=循环开始的索引形式,
1=在endforeach上运行一次循环添加
退出;
死亡;
在endforeach上添加
退出;
死亡;
太棒了,乔治!将在5分钟内勾选绿色;-)@michaelmcgurk没问题Michael。太棒了,乔治!将在5分钟内勾选绿色;-)@michaelmcgurk没问题Michael。