2014-10-09 103 views
1

我正在嘗試創建一個查詢,以按字母順序顯示分類中的所有術語。使用foreach創建一個按字母順序顯示術語的查詢

這裏是我的代碼,到目前爲止,單個字母:

<?php $args = array('name__like' => "a", 'order' => 'ASC'); 
    $terms = get_terms('pa_manufacturer', $args); 
    if (!empty($terms) && !is_wp_error($terms)) { 
     $count = count($terms); 
     $i=0; 
     $term_list = '<ul class="my_term-archive">'; 
     echo '<h2 class="term-letter">A</h2>'; 
     foreach ($terms as $term) { 
      $i++; 
      $term_list .= '<li><a href="' . get_term_link($term) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></li>'; 
      if ($count != $i) { 
       $term_list .= ''; 
      } 
      else { 
       $term_list .= '</ul>'; 
      } 
     } 
     echo $term_list; 
    } 
    ?> 

的問題是,目前,我需要手動填寫「name__like」的一部分,與H2術語字母一起。

我想在一個查詢中返回字母表中的每個字母,動態地更改名稱___和h2文本。

否則我需要爲每個字母做一個查詢,並有25個不完全有效。

我實際上沒有太多的運氣,所以我希望得到一些建議或幫助。

任何人都可以幫忙嗎?

感謝

回答

2

get_termsname__like參數在WordPress的3.7改變。它不會檢索帶有指定第一個字母的術語名稱,但會檢索術語名稱(如果它具有指定的字母)在名稱中的任何位置

此外,如果您要嘗試運行代碼26次,要和頁面加載時間

問題,這是我的方法和檢測結果的

測試結果: 1查詢中0.01074秒。

測試輸出:請注意,我的測試網站是在南非,所有的名稱是項名稱

enter image description here

這裏,使得它發生的代碼

$terms = get_terms('pa_manufacturer'); 

if (!empty($terms) && !is_wp_error($terms)){  
$term_list = [];  
foreach ($terms as $term){ 
    $first_letter = strtoupper($term->name[0]); 
    $term_list[$first_letter][] = $term; 
} 
unset($term); 

echo '<ul class="my_term-archive">'; 

    foreach ($term_list as $key=>$value) { 
     echo '<h2 class="term-letter">' . $key . '</h2>'; 

     foreach ($value as $term) { 
      echo '<li><a href="' . get_term_link($term) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></li>'; 
     } 
    } 

echo '</ul>'; 
} 

代碼如何運作

這裏最好的辦法是一次性得到所有的術語,以避免不必要的數據庫命中。您現在可以通過foreach循環從get_terms傳遞結果。

在這裏你要訴諸你的條款。首先要做的是創建一個新數組$term_list,獲取每個術語名稱的第一個字母,然後將該字母作爲新數組中的一個鍵。每個鍵的值將包含一個術語信息數組

這個新數組將用於在每個字母下顯示術語名稱,如上圖所示。

+0

太棒了,謝謝!我真的很喜歡它:) – 2014-10-10 09:36:31

+0

我的榮幸,很高興它成爲了你。請享用 :-) – 2014-10-10 10:28:33

0

嘗試類似的東西(可能需要的調整 - 未經測試的書面)

<?php $args = array('order' => 'ASC'); 
    $terms = get_terms('pa_manufacturer', $args); 
    if (!empty($terms) && !is_wp_error($terms)) { 
    $count = count($terms); 
    $i=0; 
    $term_list = '<ul class="my_term-archive">'; 
    $letter = "A"; 

    foreach ($terms as $term) { 
     $i++; 

     if($letter !== strtoupper($term->name[0]) { 
     $letter= strtoupper($term->name[0]); 
     echo "<h2 class="term-letter">{$letter}</h2>"; 
     } 
     $term_list .= '<li><a href="' . get_term_link($term) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></li>'; 
     if ($count != $i) { 
      $term_list .= ''; 
     } 
     else { 
      $term_list .= '</ul>'; 
     } 
    } 
    echo $term_list; 
} 
?>` 
+0

此代碼顯示字母列表,但不顯示每個字母下的字詞。它顯示了完整的字母表中的所有條款。 – 2014-10-09 14:10:54

相關問題