WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] get_the_tags issue

[Resolved] get_the_tags issue

  • I’m trying to get the tags from within the Loop and use them as variables. It’s all working kind of okay – I just keep getting this warning:

    “Warning: Invalid argument supplied for foreach()…”

    I’m using this, from the codex, and from what I can see, I’m implementing it as I’m supposed to, but that warning will *not* go away.

    The piece of code that I’ve written:

    foreach((get_the_tags()) as $tag) {
            $posttag = $tag->slug;
            }

    This is *definitely* within the Loop. The posts display as they should – so it’s calling in the variables correctly – but I still get that stinkin’ warning on the page. I’m thinking I need another set of eyes – would anyone know why it’s doing this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’d say the problem is no tags for whatever is the ‘current post’, thus the array is empty and the cause of your error.

    Didn’t test this but try something like:

    <?php
    $mytags = get_the_tags();
    if ($mytags) {
    foreach($mytags as $tag) {
    $posttag = $tag->slug;
    }
    }
    ?>

    That’s odd – because there *are* tags for the posts in question. (Further proof is that they’re actually showing up, too – they just have the warning above them – and I’m working on a Tag archive page.)

    I see what you’re doing there though. And hey – it worked!

    I wonder if it’s because I have *other* posts associated with the stuff I’m bringing in, and *those* are the ones that don’t have any tags. That could certainly be it – and I never even thought of that coming into the picture.

    Thanks so much Michael!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] get_the_tags issue’ is closed to new replies.