Reputation: 13
I have this PHP code that produces a warning:
Warning: Invalid argument supplied for foreach() php wordpress
Here is the code:
<?php
$post_status1 = 'publish';
$post_type1 = 'page';
$featucat = "about";
$featucount = "1";
$my_query = new WP_Query('post_status='. $post_status1 .'&post_type='. $post_type1.'');
if ($my_query->have_posts()){
while ($my_query->have_posts()) : $my_query->the_post();
$front_values = get_post_custom_values('Homepage_Blog_01p', get_the_ID());
foreach ( $front_values as $front_key => $result_value ) {
if($result_value == 'about') {
?>
<div class="thewidgets">
<?php
$description_values = get_post_custom_values('Description_Field', get_the_ID());
foreach ( $description_values as $description_key => $description_value ) {
echo $description_value;
}
?>
<a href="<?php the_permalink(); ?>" title="Read the whole post" class="rm">Read More</a>
</div>
<?php } } endwhile; } ?>
Here is the full error:
Warning: Invalid argument supplied for foreach() in
D:\PROGRAM FILES\wamp\www\westchester\wp-content\themes\
computerrepair\footer.php on line 22"
What am I doing wrong?
Upvotes: 0
Views: 5402
Reputation: 59
<?php wp_head(); ?>
Add this code in the head tag. I think it will solve your warning - Warning: Invalid argument supplied for foreach() in C:\wamp64\www\development\wp-includes\script-loader.php on line 2652
Upvotes: 0
Reputation: 153922
Put this in a.php
:
<?php
$skipper = "abcd";
foreach ($skipper as $item){ //warning happens on this line.
print "ok";
}
?>
Prints:
eric@dev ~ $ php a.php
PHP Warning: Invalid argument supplied for foreach() in
/var/www/sandbox/eric/code/php/run06/a.php on line 3
PHP Stack trace:
The warning means exactly what it says. You passed a parameter into the foreach structure which could not be evaluated in the foreach. Before the foreach loop, ensure that the first parameter is a structure that the foreach can handle.
Upvotes: 0
Reputation: 180024
$front_values
is not an array if you're getting that. Check its contents, and if it's legitimately not an array at times (for example, if get_post_custom_values
returns null
when there aren't any results), account for it by wrapping the foreach
in an if(is_array($front_values)) {
conditional.
Upvotes: 1