Wordpress,foreach里面的foreach,重复的信息

时间:2015-07-02 22:20:01

标签: php wordpress loops foreach duplicates

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

我有2个提交的自定义元

2 filed custom meta

以这种方式显示:

only 2 image placed

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

第一个和第三个<li>只是视频,第二个和第四个工作正常,看起来像这样:

enter image description here

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>';
}

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。

提交的自定义字段:prntscr.com/7oqqvt

我将它们命名为“name_1”,“name_2”......“name_4”。 $ i等于自定义字段中的最后一个nubmer。

屏幕上的结果: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;

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

谢谢,格雷格,有点暗示,但我选择“而不是”而不是“for”!祝你好运

答案 1 :(得分:0)

不要在foreach循环中使用foreach。这是一个功能问题。 使用替代&#34; for&#34;循环而不是双foreach。