Wordpress,foreach inside foreach,重复信息

Wordpress, foreach inside foreach, duplicated info

我需要一些帮助来解决重复问题。成交:

我有 2 个归档的自定义元数据

显示是这样的:

只放置了 2 张图片,但重复了 <li>

第一和第三 <li> 只是视频,第二和第四很好用,看起来像这样:

foreach($slider_xml->childNodes as $slider){
$test_test = get_post_meta(9,'test_1', false);
foreach($test_test as $test_test){
echo '<li class="img-vid-slide">';
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>'; 
}
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
$image_full_url = wp_get_attachment_image_src(find_xml_value($slider, 'image'), 'full');
echo '<a href="' . $image_full_url[0] . '" data-rel="prettyPhoto[flexgal]" title=""  >';
}else if($link_type != 'No Link'){
echo '<a href="' . $link . '" >';
}
echo '<img class="'. $i .'" src="' . $image_url[0] . '" alt="' . $alt_text . '" />';
echo '</div>';
if( !empty($title) ){
echo '<div class="flex-caption gdl-slider-caption">';
echo '<div class="gdl-slider-title">' . $title . '</div>';
echo '<div class="slider-title-bar"></div>';
echo $caption;
echo '</div>'; // flex-caption
}
if($link_type != 'No Link'){
echo '</a>';
}
echo '</li>';
}

不要在 foreach 循环中使用 foreach。这是一个功能问题。 使用替代的 "for" 循环而不是双 foreach。

我找到了解决方案。

已归档自定义字段:prntscr.com/7oqqvt

我将它们命名为 "name_1"、"name_2"..."name_4"。 $i 等于自定义字段中的最后一个数字。

屏幕上的结果:prntscr.com/7oqsaw

$i = 1;
while ($i <= 3):
foreach( $slider_xml->childNodes as $slider ){
// ======= SAME AS FIRST ====== //  
$test_test = get_post_meta(9,'test_'. $i .'', true);    
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>'; 
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
// ======= SAME AS FIRST ====== //
}
echo '</li>';
$i++;       
}       
endwhile;

对我来说很好,做我正在寻找的!

谢谢 Greg 的一点提示,但我选择 "while" 而不是 "for"!祝你好运