I would like to sort a tag list by alphabet with their corresponding letter. including the empty ones.
At the moment I'm listing only the letter that has tags but can't get them to display/sort alphabetically. Then I also have the issue of it not showing the empty letter in the alphabet. Also worth mentioning I added the asort() because it wasn't sorting correctly.
Working:
- adding an alphabetic separator and nesting its tags under it
Not working:
- sorting alphabetically
- adding empty alphabetic number as well with text "No tags to display for this letter"
This is what I have so far:
<?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>
Its displaying like this at the moment: Tagwrap: Tagwrap output
Tag List: Same sorting order as the above, its pretty much using the same php.
Sorting the terms is not going to make a difference to what you are trying to do. If you want to include letters that are not in your results, then you can't use the results for your loop regardless of how it's sorted, because it can only loop through what's there.
Instead, we can simply loop on the alphabet and use the letter as the key to get the terms from your array.
First, loop through the alphabet to display the letters in your
tag-wrapdiv:Now we will again loop through the alphabet, and this time get the results from your
$term_listusing the letter as the key. If there are no terms, then you can display a message, otherwise you can display the list as you were before.(Note: This code is untested but the general idea is there)
This also means that you don't have to worry about doing any sorts on your arrays because we are using the alphabet loop to sort thedisplay/