WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_the_tags issue (3 posts)

  1. Doodlebee
    Member
    Posted 6 years ago #

    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?

  2. MichaelH
    Member
    Posted 6 years ago #

    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;
    }
    }
    ?>
  3. Doodlebee
    Member
    Posted 6 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.