1

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

目前我只列出有标签但不能让它们按字母顺序显示/排序的字母。然后我也有它没有在字母表中显示空字母的问题。另外值得一提的是,我添加了 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 output

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

4

2 回答 2

1

对术语进行排序不会对您尝试做的事情产生影响。如果您想包含结果中没有的字母,那么无论结果如何排序,您都不能将结果用于循环,因为它只能遍历那里的内容。

相反,我们可以简单地循环字母表并使用字母作为键从数组中获取术语。

首先,遍历字母表以显示tag-wrapdiv 中的字母:

$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>

(注意:此代码未经测试,但总体思路是存在的)

这也意味着您不必担心对数组进行任何排序,因为我们使用字母循环对显示/

于 2020-06-26T09:19:01.037 回答
0

这是基于 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>
于 2020-06-30T08:27:52.153 回答