タグクラウド内で現在閲覧中のページに付けられたタグをハイライトする

Ads

Code

<?php
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;
}
//current-tagというclassが追加される
?>
view raw gistfile1.php hosted with ❤ by GitHub

Screen shot

Note

Description 現在閲覧中のページに付けられているタグを取得してタグクラウド内のタグのclassを任意のclassを含めたものに置換する
WordPress Ver. 3.5
Via Tag Cloud Filter to Show Current Tags