0

有没有办法可以添加weighted tags到wordpress?我需要自定义tag cloud小部件以使用不同颜色显示最常用的标签

4

1 回答 1

2

我也偶然发现了这一点。这是原始标签云小部件的修改功能:

<?php 
function my_colorful_tag_cloud( $args = array() ) {
    $defaults = array(
        'smallest' => 10, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
        'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
        'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
    );
    $args = wp_parse_args( $args, $defaults );

    $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags

    if ( empty( $tags ) || is_wp_error( $tags ) )
        return;

    foreach ( $tags as $key => $tag ) {
        $link = get_term_link( intval($tag->term_id), $tag->taxonomy );

        if ( is_wp_error( $link ) )
            return false;

        $tags[ $key ]->link = $link;
        $tags[ $key ]->id = $tag->term_id;
    }
    $defaults = array(
        'smallest' => 10, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
        'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
        'topic_count_text_callback' => 'default_topic_count_text',
        'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
    );

    if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
        $body = 'return sprintf (
            _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
            number_format_i18n( $count ));';
        $args['topic_count_text_callback'] = create_function('$count', $body);
    }

    $args = wp_parse_args( $args, $defaults );
    extract( $args );

    if ( empty( $tags ) )
        return;

    $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
    if ( $tags_sorted != $tags  ) { // the tags have been sorted by a plugin
        $tags = $tags_sorted;
        unset($tags_sorted);
    } else {
        if ( 'RAND' == $order ) {
            shuffle($tags);
        } else {
            // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
            if ( 'name' == $orderby )
                uasort( $tags, '_wp_object_name_sort_cb' );
            else
                uasort( $tags, '_wp_object_count_sort_cb' );

            if ( 'DESC' == $order )
                $tags = array_reverse( $tags, true );
        }
    }

    if ( $number > 0 )
        $tags = array_slice($tags, 0, $number);

    $counts = array();
    $real_counts = array(); // For the alt tag
    foreach ( (array) $tags as $key => $tag ) {
        $real_counts[ $key ] = $tag->count;
        $counts[ $key ] = $topic_count_scale_callback($tag->count);
    }

    $min_count = min( $counts );
    $spread = max( $counts ) - $min_count;
    if ( $spread <= 0 )
        $spread = 1;
    $font_spread = $largest - $smallest;
    if ( $font_spread < 0 )
        $font_spread = 1;
    $font_step = $font_spread / $spread;

    $a = array();
    $colors = 6;

    foreach ( $tags as $key => $tag ) {
        $count = $counts[ $key ];
        $real_count = $real_counts[ $key ];
        $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
        $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
        $tag_name = $tags[ $key ]->name;
        $class = 'color-' . ( round( ( $smallest + ( ( $count - $min_count ) * $font_step ) ) - ( $smallest - 1 ) ) );
        $a[] = "<a href='$tag_link' class='tag-link-$tag_id $class' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
            str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
            . "$unit;'>$tag_name</a>";
    }

    $return = join( $separator, $a );

    return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
}

此代码(默认情况下)将为每个标签生成一个标签云,其中包含 13 个可能的类(因为默认值是 min.size=10 和 max.size =22)。为了更改每个标签权重的颜色,只需将这些规则添加到您的 CSS 中:

.colorful_tag_cloud a.color-x { color: {color} }

其中“x”是从 1 到 13 的数字

希望这可以帮助。

于 2012-12-21T18:09:21.710 回答