我目前正在尝试向Tag Cloud小部件中的链接添加样式,我可以看到它的用途wp_tag_cloud()
输出云本身。wp\\u tag\\u cloud()中的参数没有包含我想要的类名,因为设置title属性的样式效率不高,也不允许有太大的增长。例如:
.widget .tagcloud a[title~="1"]{
color: red;
}
.widget .tagcloud a[title~="2"]{
color: yellow;
}
.widget .tagcloud a[title~="9999"]{
color: purple;
}
我找到了
wp_generate_tag_cloud_data
过滤器,看起来是我想要的,但我必须错过一些东西,因为它没有任何效果。以下是我目前掌握的情况:
add_filter( \'wp_generate_tag_cloud_data\', \'my_tag_cloud_data\', 10, 1 );
function my_tag_cloud_data($tags_data){
foreach ( $tags as $key => $tag ) {
$tag_id = isset( $tag->id ) ? $tag->id : $key;
$tag_class= \'tag-link-\' . $tag_id;
$count = $counts[ $key ];
$real_count = $real_counts[ $key ];
if ($real_count > 20){
$tag_class= \'tag-link-\' . $tag_id . \' x-large\';
} elseif ($real_count > 15){
$tag_class= \'tag-link-\' . $tag_id . \' large\';
} elseif ($real_count > 7){
$tag_class= \'tag-link-\' . $tag_id . \' medium\';
} elseif ($real_count > 1){
$tag_class= \'tag-link-\' . $tag_id . \' small\';
} else {
$tag_class= \'tag-link-\' . $tag_id . \'x-small \';
}
if ( $translate_nooped_plural ) {
$title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
} else {
$title = call_user_func( $args[\'topic_count_text_callback\'], $real_count, $tag, $args );
}
$tags_data[] = array(
\'id\' => $tag_id,
\'url\' => \'#\' != $tag->link ? $tag->link : \'#\',
\'name\' => $tag->name,
\'title\' => $title,
\'slug\' => $tag->slug,
\'real_count\' => $real_count,
\'class\' => $tag_class,
\'font_size\' => $args[\'smallest\'] + ( $count - $min_count ) * $font_step,
);
}
return $tags_data;
}
我尝试过使用优先级并将其包装到绑定到init挂钩的函数中,但没有效果。如果有人能告诉我我做错了什么,我将不胜感激。
最合适的回答,由SO网友:jgraup 整理而成
您只想根据计数添加类名。您上面的代码看起来像是从某处复制/粘贴的,但您并不需要所有这些。
我刚刚用wp_generate_tag_cloud_data
(#L869) 和wp_tag_cloud()
它正在发挥作用。
不幸的是,对于像我这样的基本测试站点count
表示我的最大标记数。您可能希望根据标准化的font_size
. 基本上,做一些数学运算,将其转换为0-1,然后选择具有该值的类--而不是count
.
add_filter( \'wp_generate_tag_cloud_data\', \'my_tag_cloud_data\', 10, 1 );
function my_tag_cloud_data( $tags_data ) {
foreach ( $tags_data as $key => $tag ) {
// get tag count
$count = $tag [ \'real_count\' ];
// adjust the class based on the size
if ( $count > 20 ) {
$tags_data [ $key ] [ \'class\' ] .= \' tag x-large\';
} elseif ( $count > 15 ) {
$tags_data [ $key ] [ \'class\' ] .= \' tag large\';
} elseif ( $count > 7 ) {
$tags_data [ $key ] [ \'class\' ] .= \' tag medium\';
} elseif ( $count > 1 ) {
$tags_data [ $key ] [ \'class\' ] .= \' tag small\';
} else {
$tags_data [ $key ] [ \'class\' ] .= \' tag x-small \';
}
}
// return adjusted data
return $tags_data;
}
CSS
<style>
.tag.x-large {
color: red;
}
.tag.large {
color: green;
}
.tag.medium {
color: blue;
}
.tag.small {
color: yellow;
}
.tag.x-small {
color: orange;
}
</style>