• Resolved cpoteet

    (@cpoteet)


    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>";
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Try:

    get_tag_link( $tag->term_id )

    Also might use:

    $tags = get_the_tags();
    if ($tags) (
    foreach (tags as $tag)
    {
    //blah blah blah
    }
    }

    Thread Starter cpoteet

    (@cpoteet)

    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>";
    }
    P

    (@paulsanduleac)

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

    Thread Starter cpoteet

    (@cpoteet)

    @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(); ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get Post Tags in Dropdown’ is closed to new replies.