0

I'm nearly there with this, but I need to highlight the current tag link/archive being viewed:

<ul id="blog-tags">
            <?php 
            $tags = get_tags();
            if ($tags) {
                foreach ($tags as $tag) {
                    echo '<li><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></li>';
                }
            }
             ?>
        </ul>

I'd like to apply <li class="active-tag"> to the list item that contains the active tag in the code above - can anyone help me with that please?

Many thanks

Osu

2

Compare $tag->term_id with the value from get_queried_object_id(). You have to cast the former to integer, because it is provided as a string for no good reason.

<ul id="blog-tags">
<?php
$tags = get_tags();
if ( $tags ) {
    foreach ( $tags as $tag ) {
        echo '<li>';

        if ( (int) $tag->term_id === get_queried_object_id() )
            echo "<b>$tag->name</b>";
        else
            printf(
                '<a href="%1$s">%2$s</a>',
                get_tag_link( $tag->term_id ),
                $tag->name
            );

        echo '</li>';
    }
}
?>
</ul>
  • Nice, fav it now – JMau Aug 11 '13 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.