1

i'm stuck with a "Warning: Invalid argument supplied for foreach() in header.php on line 215" in a site i'm building and don't have the slightest clue. It must have something to do with the fact that i suck at coding.

It reads as follows. Line 215 is the 2nd one.

<div class="section-background-pagination caroufredsel-pagination">
                    <?php foreach ( $hero_slides as $i => $slide_id ) : ?>
                        <a href="#"></a>
                    <?php endforeach; ?>

Thanks to all the wise guys around here

John Page
  • 11
  • 1
  • 1
  • 2
  • How is $hero_slides set? Is it sometimes absent, do some pages not have the carousel? If so, you might need to surround the carousel code with something like: if (isset($hero_slides)): or if (!empty($hero_slides)): – Mason Nov 16 '16 at 17:02
  • Possible duplicate of [PHP Warning: Invalid argument supplied for foreach()](http://stackoverflow.com/questions/6572061/php-warning-invalid-argument-supplied-for-foreach) – Henders Nov 16 '16 at 17:09
  • this is the header.php. It is put in the homepage page as in the following upper part of the theme http://solopine.com/willow/ – John Page Nov 16 '16 at 17:21

1 Answers1

5

Looks like your $hero_slides array is empty, so foreach can't "touch" any data. Try to check your array before start foreach.

<?php if (!empty($hero_slides)) : ?>
<?php foreach ( $hero_slides as $i => $slide_id ) : ?>
<a href="#"></a>
<?php endforeach; ?>
<?php endif; ?>

You can look inside your array with print_r:

<?php print_r($hero_slides); ?>
Alex
  • 2,707
  • 4
  • 29
  • 42
  • Thanks for the answer. I'm really too unqualified to delve into coding to this level. This is basically a bug in a wordpress theme i'm installing and customizing. Is there something i could change in the line to just make the warning go off? Thanks – John Page Nov 16 '16 at 17:32
  • Well, you were able to see what code is contained in the file, right? Just change this code to the code from my answer above. – Alex Nov 16 '16 at 17:41
  • I did it but still get the error this time on line 216 which is the added If i take that out it the site collapses and i get a Parse error: syntax error, unexpected end of file in 350. line 350 doesn't seem to exist buy the last one, 348, as the same Any further advice? Thank you! – John Page Nov 16 '16 at 18:02
  • I've found these two pieces of code with $hero_slides. willow_data_printer(array('interval' => $hero_slides_interval)); ?>> $slide_id ) : ?> and the other one is $hero_slides = willow_page_option( 'hero_section.0.slides', array() ); – John Page Nov 16 '16 at 18:09
  • If you just want to hide the warning, edit wp-config.php and find the line that reads define('WP_DEBUG', true); and change it to read define('WP_DEBUG', false); That will usually suppress warnings, though sometimes not ( https://aristath.github.io/blog/wp-hide-php-errors for more). Alternatively, you can contact the theme developer and ask them to fix it. – Mason Nov 16 '16 at 22:08
  • Thanx very much, going to check that out! – John Page Dec 05 '16 at 20:05