WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get Post Tags in Dropdown (6 posts)

  1. cpoteet
    Member
    Posted 4 years ago #

    I have a function that displays the current posts tags in a dropdown that opens the category page onselect. However, I thought the code would also work for tags, but for some reason it does not. Here is the category dropdown code.

    function drop_cats()
    {
        echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
        echo "<option>Categories</option>\n";
        foreach (get_the_category() as $cat)
        {
            echo "<option value=\"";
            echo get_category_link($cat->cat_ID);
            echo "\">".$cat->cat_name."</option>\n";
        }
        echo "</select>";
    }

    Here is my attempt at the tags one, but no go.

    function drop_tags()
    {
        echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
        echo "<option>Tags</option>\n";
        foreach (get_the_tags() as $tag)
        {
            echo "<option value=\"";
            echo get_tag_link($tag->tag_ID);
            echo "\">".$cat->cat_name."</option>\n";
        }
        echo "</select>";
    }
  2. MichaelH
    Member
    Posted 4 years ago #

    Try:

    get_tag_link( $tag->term_id )
  3. MichaelH
    Member
    Posted 4 years ago #

    Also might use:

    $tags = get_the_tags();
    if ($tags) (
    foreach (tags as $tag)
    {
    //blah blah blah
    }
    }
  4. cpoteet
    Member
    Posted 4 years ago #

    Thanks Michael! Here is my final code if anyone else is interested.

    function drop_tags()
    {
        echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
        echo "<option>Tags</option>\n";
        foreach (get_the_tags() as $tag)
        {
            echo "<option value=\"";
            echo get_tag_link($tag->term_id);
            echo "\">".$tag->name."</option>\n";
        }
        echo "</select>";
    }
  5. paulsanduleac
    Member
    Posted 4 years ago #

    How do you call it in the theme? Just write this code in <?php ?> ?

  6. cpoteet
    Member
    Posted 4 years ago #

    @paulsanduleac: Put the function above in your theme's functions.php file (if it doesn't have one then created it). Then place the following function in the loop where you want it.

    <?php drop_tags(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic