2012-04-20 45 views
0

我有大量的WordPress帖子,每個帖子都有兩個標籤,一個標籤是狀態,另一個標籤是機場代碼。如果包含小寫字母,則從數組中刪除

我能夠生成利用這裏的指示標籤的下拉菜單:http://www.wprecipes.com/wordpress-hack-display-your-tags-in-a-dropdown-menu

但是,我想實際上有兩個不同的下拉列表中,一個列出了各國字母順序,其他列表按順序排列機場。每個機場將總是三個大寫字母。是否有一個我可以補充的論點,以便我可以爲機場創建一個下拉菜單,爲另一個國家創建另一個下拉菜單?

如果它包含一個小寫字母,它會進入狀態下拉菜單。如果不是小寫字母,那就是機場。

+0

由於我不是美國人,你能舉個例子標籤去哪裏嗎? – ariefbayu 2012-04-20 02:56:48

+0

當然,所以一個帖子將被標記爲「內華達州」,「LAS」。內華達州是州,LAS是機場代碼。 「佛羅里達」,「MCI」是另一個例子。各州在標籤上始終有一個小寫字母,機場永遠不會。 – brianrhea 2012-04-20 03:06:54

回答

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 =狀態

只是嘗試一下,並告訴我,如果這是你想要的。

+0

是啊,你釘了它!謝謝,不必修改一件事情。 – brianrhea 2012-04-20 03:35:19

相關問題