drze7794 2012-04-20 02:40
浏览 53
已采纳

如果包含小写字母,则从数组中删除

I have a number of WordPress posts that will each have two tags, one tag is the State and the other tag is an Airport Code.

I'm able to generate a dropdown menu from the tags using the instructions here: http://www.wprecipes.com/wordpress-hack-display-your-tags-in-a-dropdown-menu

But, I would like to actually have two different dropdowns, one that lists the States in alpha order, the other lists the Airports in alpha order. Every Airport will always be three uppercase letters. Is there an argument that I can add to to this so that I can create one dropdown for Airports and another for States?

If it contains a lowercase letter, it goes in the State dropdown. If no lowercase, it's an airport.

  • 写回答

1条回答 默认 最新

  • dongxin5429 2012-04-20 03:31
    关注

    I modify the snippet from your attached tutorials into something like this:

    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'>
    \t<li>";
            $return .= join("</li>
    \t<li>", $a);
            $return .= "</li>
    </ul>
    ";
            break;
        default :
            $return = join("
    ", $a);
            break;
        endswitch;
    
        return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );
    }
    

    Basically, I was just adding new parameter called tags_mode with the following parameter supported:

    all
    airport
    state
    

    And then, in dropdown_generate_tag_cloud(), I add this code:

        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
        }
    

    The main idea in this added snippet is this:

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

    It work like this: if the uppercased tag name is equal to the original tag name. That means, current tag is already uppercased (or, equal to airport code).

    To implement it, just do as the tutorial says. Only, you need to add new parameter:

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

    Notice the &tags_mode=state

    Just try it out and tell me if this is what you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算