Hi
I’m using costum field in wp to show image and text multiple time but after the second time it give me this warning … why?
webpage
meta
Warning: Invalid argument supplied for foreach()
<?php
$text = get_post_meta($post->ID, "text", false);
$image = get_post_meta($post->ID, "image", false);
if ($text . $image[0]=="")
{ ?>
<?php } else { ?>
<div class="Caption">
<?php
foreach($text as $text) {
foreach($image as $image){
echo '<div class="tutorial">' , '<img src="'.$image.'" width="250"" align="left" >' ;
echo '<p>'.$text.'</p>' ;
}
} ?>
</div> <?php } ?>
I’m… not a WP guru, but i’m fairly sure you shouldnt be nesting your foreach.
Also, you cant say $text as $text, or you’ve overwritten $text.
So…
Script starts, gets an array for $text.
Foreach $text as $text; the loop starts, takes the first element of $text, and sets it to $text. $text is now a string.
Output happens.
Loop ends. Foreach looks for the next element of the $text array… but $text isnt an array anymore. So it throws a warning.
I think you’re looking for something more like…
foreach($text as $key => $textcur) {
echo '<div class="tutorial">' , '<img src="'.$image[$key].'" width="250"" align="left" >' ;
echo '<p>'.$textcur.'</p>' ;
}
God bless you … work perfectly … have a fantastic day tnx
The error in your foreach script is because the foreach script expects an array as the first parameter and iterates through the array showing each string $item and possibly a numeric $key if it is mapped to the string $item.
Your foreach script first parameter is an array and uses the same array name as the second iterator parameter.
I always pluralise the first array parameter and then use the $key => $item:
Try this: (not tested 
<?php
$textS = get_post_meta($post->ID, "text", false);
$imageS = get_post_meta($post->ID, "image", false);
if ($textS . $imageS[0] !== "")
{
echo '<div class="Caption">';
foreach($textS as $text)
{
foreach($imageS as $key => $image)
{
echo '<div class="tutorial">';
echo '<img src="' .$image .'" width="250"" align="left" />' ;
echo '<p>';
echo '$key => ' .var_dump($key);
echo '</p>';
echo '<p>' .$text .'</p>';
echo '</div>'; // tutorial
}
}
echo '</div>'; // Caption
}
// using alternative syntax - do you think it is easier to understand?
if ($textS . $imageS[0] !== "") : // trailing colon
echo '<div class="Caption">';
foreach($textS as $text) : // trailing colon
foreach($imageS as $key => $image) : // trailing colon
echo '<div class="tutorial">';
echo '<img src="' .$image .'" width="250"" align="left" />' ;
echo '<p>';
echo '$key => ' .var_dump($key);
echo '</p>';
echo '<p>' .$text .'</p>';
echo '</div>'; // tutorial
endforeach;
endforeach;
echo '</div>'; // Caption
endif;
?>