Warning is "Invalid argument supplied for foreach()"
<?php
$tags = get_the_tags();
foreach($tags as $tag):?>
<a href="<?php echo get_tag_link($tag->term_id);?>" class="badge badge-success">
<?php echo $tag->name;?>
</a>
<?php endforeach;?>
2
Answers
Check if your $tags variable is an actual array of elements before trying to loop through it all!..
ie:
In general, don’t assume that a variable returned from a function will always be an array, you can check this before calling
foreach
like this:In the specific case of
get_the_tags()
, this might be enough though: