【问题标题】:Show 1st Image from an array in php在php中显示数组中的第一张图片
【发布时间】:2021-09-16 08:44:56
【问题描述】:

现在我只需要显示数组中的第一张图片,但我只需要显示第一张图片。

代码如下:

<?php $gallery = get_post_meta( get_the_ID(), CHEFSCUISINE_RECIPE_PREFIX . 'gallery', true );   ?> 
    <?php if ( has_post_thumbnail( $recipe ) ) { ?>
    <?php echo get_the_post_thumbnail( $recipe, 'full', array( 'class' => 'img-responsive' ) ); ?>
    <?php } else { ?>
                

    <?php foreach ( $gallery as $id => $src ) : ?>
      
      <img src="<?php echo esc_url( $src ); ?>" alt="gallery-image">
      
    <?php endforeach; } ?> 

【问题讨论】:

  • 将此添加到您的代码中:print_r($gallery);并将结果发布在 cmets 中

标签: php arrays wordpress


【解决方案1】:
            <?php if ( has_post_thumbnail( $recipe ) ) { ?>
            <?php echo get_the_post_thumbnail( $recipe, 'full', array( 'class' => 'img-responsive' ) ); ?>
        <?php } else { ?>
            

            <?php  ?>
             
             <img src="<?php echo $gallery[0] ; ?>" alt="gallery-image">
           
            <?php } ?>   

【讨论】:

  • 在使用$gallery[0] 之前,您可能需要确保$gallery 不为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多