0

我有许多 WordPress 帖子,每个帖子都有两个标签,一个标签是州,另一个标签是机场代码。

我可以使用此处的说明从标签生成下拉菜单:http: //www.wprecipes.com/wordpress-hack-display-your-tags-in-a-dropdown-menu

但是,我实际上希望有两个不同的下拉列表,一个以字母顺序列出州,另一个以字母顺序列出机场。每个机场将始终是三个大写字母。是否有一个我可以添加到此的论点,以便我可以为机场创建一个下拉列表,为国家创建另一个下拉列表?

如果它包含一个小写字母,它会出现在 State 下拉列表中。如果没有小写,它是一个机场。

4

1 回答 1

2

我将您所附教程中的片段修改为如下内容:

function dropdown_tag_cloud( $args = '' ) {//supported: 'all', 'airport', 'state'
    $defaults = array(
        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
        'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
        'exclude' => '', 'include' => '', 'tags_mode' => 'all'
    );
    $args = wp_parse_args( $args, $defaults );

    print_r($args);


    $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 ) ){
        echo "wp error...";
        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 ) {

        if($tags_mode == 'airport'){

            //if uppercased tag is equal to the tag
            //which means current tag already uppercased.
            if(!(strtoupper($tag->name) == $tag->name))
                continue;//skip current tag

        } else if($tags_mode == 'state'){
            //if uppercased tag is equal to the tag
            //which means current tag already uppercased.
            if((strtoupper($tag->name) == $tag->name))
                continue;//skip current 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[] = "\t<option value='$tag_link'>$tag ($count)</option>";
    }

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

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

基本上,我只是添加tags_mode了支持以下参数的新参数:

all
airport
state

然后,在 中dropdown_generate_tag_cloud(),我添加以下代码:

    if($tags_mode == 'airport'){

        if(!(strtoupper($tag->name) == $tag->name))
            continue;//skip current tag

    } else if($tags_mode == 'state'){
        if((strtoupper($tag->name) == $tag->name))
            continue;//skip current tag
    }

这个添加的片段的主要思想是:

strtoupper($tag->name) == $tag->name

它是这样工作的:如果大写的标签名称等于原始标签名称。这意味着,当前标签已经大写(或等于机场代码)。

要实现它,只需按照教程中的说明进行操作。只是,您需要添加新参数:

<?php dropdown_tag_cloud('number=0&order=asc&tags_mode=state'); ?>

注意&tags_mode=state

试试看,告诉我这是否是你想要的。

于 2012-04-20T03:31:34.997 回答