2014-10-09 102 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

相关问题