Support » Fixing WordPress » Help with get_the_tags

  • Resolved MountainTop

    (@mountaintop)


    Trying to display code based on different tag values.
    http://codex.wordpress.org/Function_Reference/get_the_tags

    If the post has the tag it works fine, it the post has no tags I get Warning: Invalid argument supplied for foreach().

    <?php
    if ($all_the_tags);
    $all_the_tags = get_the_tags();
    foreach($all_the_tags as $this_tag) {
    if ($this_tag->name == "Tag 1" ) {
    ?>
    <a href="<?php echo get_tag_link(78); ?>">Tag 1</a>
    
    <?php 	} else if ($this_tag->name == "Tag 2" ) { ?>
    
    <a href="<?php echo get_tag_link(79); ?>">Tag 2</a>
    
    <?php 	} else {
    ?>
    <!-- not tagged as one or the other -->
    <?
    }
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • change the one line to:

    if($all_the_tags)foreach($all_the_tags as $this_tag) {

    No longer get the error, but the tag link also no longer shows up on the posts.

    I don’t see any reason why this should not work:

    <?php
    $all_the_tags = get_the_tags();
    if( $all_the_tags ) foreach($all_the_tags as $this_tag) {
    if ($this_tag->name == "Tag 1" ) {
    ?>
    <a href="<?php echo get_tag_link(78); ?>">Tag 1</a>
    
    <?php 	} else if ($this_tag->name == "Tag 2" ) { ?>
    
    <a href="<?php echo get_tag_link(79); ?>">Tag 2</a>
    
    <?php 	} else {
    ?>
    <!-- not tagged as one or the other -->
    <?
    }
    }
    ?>

    Worked like a charm, thanks for the help Alchymth. Maybe I just typed it in wrong before.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help with get_the_tags’ is closed to new replies.