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
1 Answer
Reset to default 2Compare $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>