亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按字母順序顯示標簽列表,每個字母都有一個部分,包括沒有術語的空字母

按字母順序顯示標簽列表,每個字母都有一個部分,包括沒有術語的空字母

PHP
呼啦一陣風 2023-07-01 15:11:24
我想按字母表及其相應的字母對標簽列表進行排序。包括那些空的。目前,我只列出帶有標簽的字母,但無法讓它們按字母順序顯示/排序。然后我也遇到了它不顯示字母表中的空字母的問題。還值得一提的是,我添加了 asort() 因為它沒有正確排序。在職的:添加字母分隔符并將其標簽嵌套在其下不工作:按字母順序排序添加空字母數字以及文本&ldquo;沒有可顯示該字母的標簽&rdquo;這是我到目前為止所擁有的:<?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 文件。
查看完整描述

2 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

對術語進行排序不會對您想要做的事情產生影響。如果您想包含結果中沒有的字母,那么無論結果如何排序,您都不能將結果用于循環,因為它只能循環遍歷其中的內容。


相反,我們可以簡單地循環字母表并使用字母作為鍵來從數組中獲取術語。


首先,循環遍歷字母表以顯示 div 中的字母tag-wrap:


$alphabet = range('A', 'Z');   // 1. use range to get the alphabet


<div class="tag-wrap">

   <ul>

     <?php

     // 2. display the regular alphabet instead of the letters from your results

     foreach ( $alphabet as $letter ) : ?>

         <li class="border-radius">

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

         </li>

    <?php endforeach; ?>

   </ul>

</div>

現在我們將再次循環遍歷字母表,這次$term_list使用字母作為鍵來獲取結果。如果沒有術語,那么您可以顯示一條消息,否則您可以像以前一樣顯示列表。


<div class="tag-list">

    <?php

      // 3. display the results by looping through the alphabet to ensure all letters are included

      foreach ( $alphabet as $letter) : ?>

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

            <div class="term-letter">

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

            </div>

                                

            <div class="tag-items">

               <?php

                    // 4. Get the terms for this letter, if there are none show a message

                    $termsforletter = $term_list[$letter];

                    if (empty($terms for letter)) { ?>

                         <p>No Results for this letter</p>

                    <?php } 

                    else {

                        foreach ( $termsforletter as $term  ): ?>

                            <div class="tag-item">

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

                           </div>

                       <?php endforeach;

                    } ?>

             </div>

         </div>

      <?php endforeach;?>

 </div>

(注意:此代碼未經測試,但總體思路已經存在)


這也意味著您不必擔心對數組進行任何排序,因為我們使用字母循環對顯示/進行排序


查看完整回答
反對 回復 2023-07-01
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

這是基于 FluffyKitten 答案的工作,只是更改了檢查以在沒有字母術語的情況下顯示文本。


<?php

$alphabet = range('A', 'Z'); 


$args = array(

    'taxonomy' => 'post_tag',

    'hide_empty' => false,

    'order' => 'DESC',

    '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); ?>

                

<div class="tag-wrap">


 <ul>

   <?php foreach ( $alphabet as $letter ) : ?>

     <li>

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

     </li>

   <?php endforeach; ?>

 </ul>

</div>


<div class="tag-list">

<?php

 foreach ( $alphabet as $letter) : ?>

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

    <div class="term-letter">

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

    </div>

                    

    <div class="tag-items">

      <?php 

       $termsforletter = $term_list[$letter]; 

       if (empty($termsforletter )): ?>

          <div class="tag-item">

            <p>No topics matching this letter </p>

          </div>

       <?php else:

        foreach ( $termsforletter as $term ): ?>

           <div class="tag-item">

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

           </div>

                                        

        <?php endforeach;?>

      <?php endif; ?>

    </div>

  </div>

  <?php endforeach; ?>

</div>


查看完整回答
反對 回復 2023-07-01
  • 2 回答
  • 0 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號