Invalid argument supplied for foreach() in Wordpress. No clue

John Page picture John Page · Nov 16, 2016 · Viewed 8.9k times · Source

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

Answer

Alex picture Alex · Nov 16, 2016

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); ?>