php - Only show first image in foreach loop

My loop shows a list of values, and underneath them, the post title and image for the value.The problem is that because

My loop shows a list of values, and underneath them, the post title and image for the value.

The problem is that because some values are associated with multiple posts, multiple images show up. I only need one image. Is there any way to show only the first image in my foreach loop?

Here's my code:

<?php 
$the_query = new WP_Query(array(
    'post_type'     => 'post',
    'post_status'   => 'publish',
    'meta_key'      => 'colors',
));

$results = [];
while ( $the_query->have_posts() ) {

    $the_query->the_post(); 
    $credits = get_field('colors');

    if( !empty($color) ) {

        foreach( $colors as $color ) {  
           $results [$color][]=array(
               'title' => get_the_title(),
               'img' => get_field('photo')
           );
       }
    }
}

foreach ($results as $color => $posts) {

   foreach($posts as $post) {  /* This shows multiple images but I only need one */
        echo '<img src="'.$post['img']['url'].'">';
    }


   echo '<div><h2>'.$color.'</h2>';

    foreach($posts as $post) {
        echo '<div>'.$post['title'].'</div>';
    }

   echo '</div>';
}

wp_reset_postdata();?>

My loop shows a list of values, and underneath them, the post title and image for the value.

The problem is that because some values are associated with multiple posts, multiple images show up. I only need one image. Is there any way to show only the first image in my foreach loop?

Here's my code:

<?php 
$the_query = new WP_Query(array(
    'post_type'     => 'post',
    'post_status'   => 'publish',
    'meta_key'      => 'colors',
));

$results = [];
while ( $the_query->have_posts() ) {

    $the_query->the_post(); 
    $credits = get_field('colors');

    if( !empty($color) ) {

        foreach( $colors as $color ) {  
           $results [$color][]=array(
               'title' => get_the_title(),
               'img' => get_field('photo')
           );
       }
    }
}

foreach ($results as $color => $posts) {

   foreach($posts as $post) {  /* This shows multiple images but I only need one */
        echo '<img src="'.$post['img']['url'].'">';
    }


   echo '<div><h2>'.$color.'</h2>';

    foreach($posts as $post) {
        echo '<div>'.$post['title'].'</div>';
    }

   echo '</div>';
}

wp_reset_postdata();?>
Share Improve this question asked Jun 1, 2019 at 17:24 BlueHelmetBlueHelmet 1791 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Depending of what you are exactly trying to achieve accessing the first element of the $posts array may work for you. No need for the foreach loop.

echo '<img src="'.$posts[0]['img']['url'].'">';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745444886a4628002.html

相关推荐

  • php - Only show first image in foreach loop

    My loop shows a list of values, and underneath them, the post title and image for the value.The problem is that because

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信