按字母顺序显示标签列表,每个字母都有一个部分,包括没有术语的空字母

我想按字母表及其相应的字母对标签列表进行排序。包括那些空的。

目前,我只列出带有标签的字母,但无法让它们按字母顺序显示/排序。然后我也遇到了它不显示字母表中的空字母的问题。还值得一提的是,我添加了 asort() 因为它没有正确排序。

在职的:

  • 添加字母分隔符并将其标签嵌套在其下

不工作:

  • 按字母顺序排序

  • 添加空字母数字以及文本“没有可显示该字母的标签”

这是我到目前为止所拥有的:

<?php

   $args = array(

     'taxonomy' => 'post_tag',

     'hide_empty' => false,

     'order' => 'ACS',

     'orderby' => 'slug'

    );

    $terms = get_terms($args);


                       

    $term_list = [];    

    foreach ( $terms as $term ){

      $first_letter = strtoupper($term->name[0]);

      $term_list[$first_letter][] = $term;

    }

    unset($term); 

    asort($term_list );?>

                    

   <div class="tag-wrap">


     <ul>

      <?php foreach ( $term_list as $key=>$value ) : ?>

         <li class="border-radius">

           <a href="#<?php echo $key; ?>"><h3><?php echo $key; ?></h3></a>

         </li>

      <?php endforeach; ?>

     </ul>

  </div>


 <div class="tag-list">

    <?php

      asort($term_list );


      foreach ( $term_list as $key=>$value ) : ?>

         <div class="term-row" id="<?php echo $key; ?>">

            <div class="term-letter">

                <h3><?php echo $key; ?></h3>

            </div>

                                

            <div class="tag-items">

               <?php foreach ( $value as $term ): ?>

                    <div class="tag-item">

                        <a href="<?php echo get_term_link( $term );?>"><?php echo $term->name;?></a>

                    </div>

               <?php endforeach;?>

             </div>

         </div>

      <?php endforeach;?>

 </div>

目前显示如下: Tagwrap: Tagwrap 输出

标签列表:与上面相同的排序顺序,几乎使用相同的 php.ini 文件。


呼啦一阵风
浏览 89回答 2
2回答

月关宝盒

对术语进行排序不会对您想要做的事情产生影响。如果您想包含结果中没有的字母,那么无论结果如何排序,您都不能将结果用于循环,因为它只能循环遍历其中的内容。相反,我们可以简单地循环字母表并使用字母作为键来从数组中获取术语。首先,循环遍历字母表以显示 div 中的字母tag-wrap:$alphabet = range('A', 'Z');&nbsp; &nbsp;// 1. use range to get the alphabet<div class="tag-wrap">&nbsp; &nbsp;<ul>&nbsp; &nbsp; &nbsp;<?php&nbsp; &nbsp; &nbsp;// 2. display the regular alphabet instead of the letters from your results&nbsp; &nbsp; &nbsp;foreach ( $alphabet as $letter ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<li class="border-radius">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="#<?php echo $letter; ?>"><h3><?php echo $letter; ?></h3></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</li>&nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp;</ul></div>现在我们将再次循环遍历字母表,这次$term_list使用字母作为键来获取结果。如果没有术语,那么您可以显示一条消息,否则您可以像以前一样显示列表。<div class="tag-list">&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; // 3. display the results by looping through the alphabet to ensure all letters are included&nbsp; &nbsp; &nbsp; foreach ( $alphabet as $letter) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="term-row" id="<?php echo $letter; ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="term-letter">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3><?php echo $letter; ?></h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="tag-items">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 4. Get the terms for this letter, if there are none show a message&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $termsforletter = $term_list[$letter];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (empty($terms for letter)) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>No Results for this letter</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $termsforletter as $term&nbsp; ): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="tag-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php echo get_term_link( $termsforletter&nbsp; );?>"><?php echo $term->name;?></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php endforeach;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; <?php endforeach;?>&nbsp;</div>(注意:此代码未经测试,但总体思路已经存在)这也意味着您不必担心对数组进行任何排序,因为我们使用字母循环对显示/进行排序

哆啦的时光机

这是基于 FluffyKitten 答案的工作,只是更改了检查以在没有字母术语的情况下显示文本。<?php$alphabet = range('A', 'Z');&nbsp;$args = array(&nbsp; &nbsp; 'taxonomy' => 'post_tag',&nbsp; &nbsp; 'hide_empty' => false,&nbsp; &nbsp; 'order' => 'DESC',&nbsp; &nbsp; 'orderby' => 'slug');$terms = get_terms($args);&nbsp; &nbsp;$term_list = [];&nbsp; &nbsp;&nbsp;foreach ( $terms as $term ){&nbsp; &nbsp; $first_letter = strtoupper($term->name[0]);&nbsp; &nbsp; $term_list[$first_letter][] = $term;}unset($term); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div class="tag-wrap">&nbsp;<ul>&nbsp; &nbsp;<?php foreach ( $alphabet as $letter ) : ?>&nbsp; &nbsp; &nbsp;<li>&nbsp; &nbsp; &nbsp; &nbsp;<a href="#<?php echo $letter;&nbsp; ?>"><h3><?php echo $letter;&nbsp; ?></h3></a>&nbsp; &nbsp; &nbsp;</li>&nbsp; &nbsp;<?php endforeach; ?>&nbsp;</ul></div><div class="tag-list"><?php&nbsp;foreach ( $alphabet as $letter) : ?>&nbsp; <div class="term-row" id="<?php echo $letter; ?>">&nbsp; &nbsp; <div class="term-letter">&nbsp; &nbsp; &nbsp; &nbsp;<h3><?php echo $letter;?></h3>&nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class="tag-items">&nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$termsforletter = $term_list[$letter];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if (empty($termsforletter )): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="tag-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>No topics matching this letter </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;<?php else:&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $termsforletter as $term ): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="tag-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="<?php echo get_term_link( $term );&nbsp; ?>"><?php echo&nbsp; $term->name;&nbsp; ?></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <?php endforeach;?>&nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <?php endforeach; ?></div>
打开App,查看更多内容
随时随地看视频慕课网APP