Iliya
Iliya

Reputation: 21

Wordpress, foreach inside foreach, duplicated info

I need some help with solving duplicating problem. To deal:

I have 2 filed custom meta

2 filed custom meta

It displays this way :

only 2 image placed

Only 2 image placed, but it duplicate <li>.

First and third <li> is only video, second and fourth works fine, looks like this :

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

Upvotes: 0

Views: 100

Answers (2)

Iliya
Iliya

Reputation: 21

I find solution.

Filed custom fields: prntscr.com/7oqqvt

I named them like "name_1","name_2"..."name_4". $i is equal of last nubmer in custom field.

Result on screen: 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;

Works fine for me, do what i searching for!

Thanks, Greg, for a little hint, but i go for "while" instead of "for"! Good luck

Upvotes: 1

Greg
Greg

Reputation: 343

Don't use the a foreach inside a foreach loop. It a function problem. Use the alternative "for" loop instead of the double foreach.

Upvotes: 0

Related Questions