我的循环显示了一个值列表,在它们下面是该值的帖子标题和图像。
问题是,由于某些值与多个帖子关联,因此会显示多个图像。我只需要一张图片。有没有办法只显示我的foreach
环
这是我的代码:
<?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();?>