最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

archives - Highlight current tag using get_tags()

programmeradmin3浏览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

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

Share Improve this question asked Aug 11, 2013 at 12:39 OsuOsu 1,4526 gold badges25 silver badges44 bronze badges 1
  • Look at this thread : questions/16816 – JMau Commented Aug 11, 2013 at 13:05
Add a comment  | 

1 Answer 1

Reset to default 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>
发布评论

评论列表(0)

  1. 暂无评论