Loop to return all Woocommerce product tags in alphabetical order

Multi tool use
Loop to return all Woocommerce product tags in alphabetical order
i am trying to make a loop in WordPress for woocommerce theme to get out all product tags in alphabetical order
in a simple way "heading with A below it all tags starts with A letter etc."
i am using the code but its return null
<?php
$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag-
>slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
?>
2 Answers
2
You can also use a WP_Term_Query
with the Woocommerce product tag custom taxonomy, to get all related terms alphabetically by letter:
WP_Term_Query
$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>';
Tested and work.
Edit related to your comment
To limit the number tags for each letter, you will use:
$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>';
Not tested…
@MinaAmir I have made an update for this at the end of my answer. It's untested. try it and let me know if it doesn't works.
– LoicTheAztec
Jul 1 at 23:22
The get_tags()
function retrieves an array of objects for each term in the post_tag
taxonomy. WooCommerce products don't use post_tag
, they use the product_tag
taxonomy.
get_tags()
post_tag
post_tag
product_tag
The function you want to use is get_terms()
:
get_terms()
$terms = get_terms(
array(
'hide_empty' => true,
'taxonomy' => 'product_tag',
)
);
$html = '<div class="post_tags">';
if($terms){
foreach($terms as $term){
$term_link = get_term_link( $term->term_id, 'product_tag' );
$html .= "<a href='" . $term_link . "' title='" . $term->name . " Tag' class='" . $term->slug . "'>" . $term->name . "</a>";
}
}
$html .= "</div>";
echo $html;
If you need then in alphabetical order, check out get_terms_orderby()
get_terms_orderby()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
thank you so much it's working with me but i need now to filter output to 1 for every letter when i put 'number' => '1' in agrs return only letter A and stops
– Mina Amir
Jul 1 at 21:41