Search code examples
phpwordpresswoocommercegroupingtaxonomy-terms

Custom taxonomy grouped terms by letter alphabetically in WooCommerce


I have a brand taxonomy for products in woocommerce. I need to sort products by brand .
I'm interested how to make the following by code:

like that

Any ideas?


Solution

  • To display linked taxonomy terms grouped by letter alphabetically, you can use the following (defining the right custom taxonomy in the code below):

    $taxonomy = 'product_brand'; // <== Here define your custom taxonomy
    $sorted   = array(); // Initializing
    
    // Get all terms alphabetically sorted
    $terms = get_terms( array(
        'taxonomy'   => $taxonomy,
        'hide_empty' => true,
        'orderby'    => 'name'
    ) );
    
    // Loop through the array of WP_Term Objects
    foreach( $terms as $term ) {
        $term_name    = $term->name;
        $term_link    = get_term_link( $term, $taxonomy );
        $first_letter = strtoupper($term_name[0]);
        
        // Group terms by their first starting letter
        if( ! empty($term_link) ) {
            $sorted[$first_letter][] = '<li><a href="'.$term_link.'">'.$term_name.'</a></li>';
        } else {
            $sorted[$first_letter][] = '<li>'.$term_name.'</li>';
        }
    }
    
    // Loop through grouped terms by letter to display them by letter
    foreach( $sorted as $letter => $values ) {
        echo '<div class="tax-by-letter">
        <h3 class="tax-letter-'.$letter.'">'.$letter.'</h3>
        <ul>' . implode('', $values) . '</ul>
        </div>';
    }
    

    It works for any taxonomy or custom taxonomy (better for non hierarchical taxonomies).

    Now this can be embedded in a Shortcode for easier usage:

    add_shortcode( 'terms_by_letter', 'display_terms_by_letter' );
    function display_terms_by_letter( $atts ) {
        // Shortcode Attributes
        extract( shortcode_atts( array(
            'taxonomy' => 'product_brand', // <== Here define your taxonomy
        ), $atts, 'terms_by_letter' ) );
    
        $sorted   = array(); // Initializing
        $output   = ''; // Initializing
    
        // Get all terms alphabetically sorted
        $terms = get_terms( array(
            'taxonomy'   => $taxonomy,
            'hide_empty' => true,
            'orderby'    => 'name'
        ) );
    
        // Loop through the array of WP_Term Objects
        foreach( $terms as $term ) {
            $term_name    = $term->name;
            $term_link    = get_term_link( $term, $taxonomy );
            $first_letter = strtoupper($term_name[0]);
    
            // Group terms by their first starting letter
            if( ! empty($term_link) ) {
                $sorted[$first_letter][] = '<li><a href="'.$term_link.'">'.$term_name.'</a></li>';
            } else {
                $sorted[$first_letter][] = '<li>'.$term_name.'</li>';
            }
        }
    
        // Loop through grouped terms by letter to display them by letter
        foreach( $sorted as $letter => $values ) {
            $output .= '<div class="tax-by-letter">
            <h3 class="tax-letter-'.$letter.'">'.$letter.'</h3>
            <ul>' . implode('', $values) . '</ul>
            </div>';
        }
        return $output;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Shortcode usage:

    [terms_by_letter] 
    

    or inside PHP code:

    echo do_shortcode('[terms_by_letter]');