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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值