Вы также можете использовать WP_Term_Query
с пользовательской таксономией тега продукта Woocommerce, чтобы получить все связанные термины в алфавитном порядке:
$taxonomy = 'product_tag';
$tags_html = [];
$tquery = new WP_Term_Query( array(
'taxonomy' => $taxonomy,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
) );
// 1st Loop: Go through each term and format it
foreach($tquery->get_terms() as $term){
$link = get_term_link( $term->term_id, $taxonomy );
$letter = $term->slug[0];
// Set alphabetically by letter each product tag formatted html (with the class, the link and the count (optionally)
$tags_html[$letter][] = '<a class="'.$term->slug.'" href="'.$link.'">'.$term->name.' ('.$term->count.')'.'</a>';
}
echo '<div class="product_tags">';
// 2nd Loop: Display all formatted product tags grouped by letter alphabetically
foreach( $tags_html as $letter => $values ){
echo '<div class="letter-'.$letter.'">Letter '.strtoupper($letter).': '.implode(' - ', $values).'</div>';
}
echo '</div>';
Проверено и работает.
Изменить, связанные с вашим комментарием
Чтобы ограничить количество тегов для каждой буквы, вы будете использовать:
$taxonomy = 'product_tag';
$tags_html = [];
$tquery = new WP_Term_Query( array(
'taxonomy' => $taxonomy,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
) );
// 1st Loop: Go through each term and format it
foreach($tquery->get_terms() as $term){
$link = get_term_link( $term->term_id, $taxonomy );
$letter = $term->slug[0];
// Get the existing array keys for a letter
$keys = isset($tags_html[$letter]) ? array_keys($tags_html[$letter]) : array();
// Limit to 5 items by letter
if( sizeof($keys) < 5 ){
// Set alphabetically by letter each product tag formatted html (with the class, the link and the count (optionally)
$tags_html[$letter][] = '<a class="'.$term->slug.'" href="'.$link.'">'.$term->name.' ('.$term->count.')'.'</a>';
}
}
echo '<div class="product_tags">';
// 2nd Loop: Display all formatted product tags grouped by letter alphabetically
foreach( $tags_html as $letter => $values ){
echo '<div class="letter-'.$letter.'">Letter '.strtoupper($letter).': '.implode(' - ', $values).'</div>';
}
echo '</div>';
Не проверено ...