首页    >    新闻资讯    >   把Tag做成下拉菜单样式

把Tag做成下拉菜单样式

有些人不喜欢Tag Cloud,觉得那样很丑。那不然试试下拉菜单样式?

这次的代码比较长,老样子,还是复制进functions.php里:

<?php
function dropdown_tag_cloud( $args = '' ) {
    $defaults = array(
        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
        'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
        'exclude' => '', 'include' => ''
    );
    $args = wp_parse_args( $args, $defaults );

    $tags = get_tags( array_merge($args,

array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags

    if ( empty($tags) )
        return;

    $return = dropdown_generate_tag_cloud( $tags, $args );

// Here's where those top tags get sorted according to $args
    if ( is_wp_error( $return ) )
        return false;
    else
        echo apply_filters( 'dropdown_tag_cloud', $return, $args );
}

function dropdown_generate_tag_cloud( $tags, $args = '' ) {
    global $wp_rewrite;
    $defaults = array(
        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
        'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
    );
    $args = wp_parse_args( $args, $defaults );
    extract($args);

    if ( !$tags )
        return;
    $counts = $tag_links = array();
    foreach ( (array) $tags as $tag ) {
        $counts[$tag->name] = $tag->count;
        $tag_links[$tag->name] = get_tag_link( $tag->term_id );
        if ( is_wp_error( $tag_links[$tag->name] ) )
            return $tag_links[$tag->name];
        $tag_ids[$tag->name] = $tag->term_id;
    }

    $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;

    // SQL cannot save you; this is a second (potentially different) sort

on a subset of data.
    if ( 'name' == $orderby )
        uksort($counts, 'strnatcasecmp');
    else
        asort($counts);

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

    $a = array();

    $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';

    foreach ( $counts as $tag => $count ) {
        $tag_id = $tag_ids[$tag];
        $tag_link = clean_url($tag_links[$tag]);
        $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
        $a[] = " <option value='$tag_link'>$tag ($count)</option>";
    }

    switch ( $format ) :
    case 'array' :
        $return =& $a;
        break;
    case 'list' :
        $return = "<ul class='wp-tag-cloud'>
<li>";
        $return .= join("</li>
<li>", $a);
        $return .= "</li>
</ul>
";
        break;
    default :
        $return = join("
", $a);
        break;
    endswitch;

    return apply_filters( 'dropdown_generate_tag_cloud', $return,

$tags, $args );
}
?>

然后,把以下代码粘贴到希望显示的位置(通常是sidebar.php):

<select name="tag-dropdown"

onchange="document.location.

href=this.options[this.selectedIndex].value;">
    <option value="#">Liste d'auteurs</option>
    <?php dropdown_tag_cloud('number=0&order=asc'); ?>
</select>

大功告成!

 

分类:新闻资讯

标签:, ,

* 版权声明:作者WordPress啦! 转载请注明出处。