Quantcast
Channel: Michael (aka alchymyth) – TransformationPowerTools
Viewing all articles
Browse latest Browse all 12

Tag Cloud Filter to Show Current Tags

$
0
0

Wouldn’t it be nice to see the current tags reflected in the tag cloud widget?
WordPress does not output any css classes for the current tags – as it would do for instance in the category or page menu.

A few lines added to functions.php of the currently used theme can change that:

add_filter ( 'wp_tag_cloud', 'tag_cloud_current_tag_highlight' );
function tag_cloud_current_tag_highlight( $return ) {
  $post_tags = array();
  if(is_single()) {
    global $post;
    $post_tags = get_the_terms($post->ID,'post_tag');
  }
  if(is_tag()) {
  	$tags = explode( '+', get_query_var('tag') );
	foreach( $tags as $tag ) { $post_tags[] = get_term_by('slug',$tag,'post_tag'); }
  }
  if( $post_tags ) {
	  foreach ($post_tags as $pt) {
      $tag = $pt->term_id;
        if(preg_match("#-link-" . $tag . "' #", $return)) {
        $return = str_replace("link-" . $tag . "' ", "link-" . $tag . " current-tag' ", $return);
        }
      }
	}
return $return;
}

With some formatting in style.css like the following, the current tags of the tag archive or the viewed single post can be highlighted:

.current-tag { font-style: italic; }

See the effect in the tag cloud lower down in the left sidebar of this site.


Viewing all articles
Browse latest Browse all 12

Trending Articles