I need to divide a set of taxonomy terms by the first letter of the term name. I found a few bits of code that may help but when I implement my code I get no return values. I'm sure it has to be something fairly small in my code but I just cannot find it.

This taxonomy contains brands and labels carried by shops listed on my site. So the number of terms is quite large. Some of the labels/brands/terms start with numbers, other letters and others are foreign characters like Russian and could be Japanese, Arabic etc as the site grows

The following code is just my first run to show only the terms that begin with Letters.

$letters = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$numbers = array(0,1,2,3,4,5,6,7,8,9);
$chracters = array_merge( $letters, $numbers );

$args = array( 'orderby' => 'name', 'order' => 'asc' );

$terms = get_terms( $taxonomy, $args );

if( count($terms) > 0 ):    

    foreach( $terms as $term ):
        $firstchar = (string) substr( $term->name, 0 );
        $firstchar = strtoupper( $firstchar );
        $termlink = get_term_link( $term->slug, $taxonomy );
            if( in_array($firstchar,$letters) ):

            <div class="post-box alignleft">                    
                <p class="post-title"><a href="<?php echo $termlink; ?>"><?php echo $term->name; ?></a></p>
            <!-- /end .post-box -->

            endif;//is a letter
    endforeach;//$terms as $term


I am not sure what is causing terms not to display. Here is the link to the development server http://obeliskwebdesign.com/sandbox/directorysexy/in-stock-sort/

Thanx for any insights or help. I've been stumped.


$taxonomy is possibly not set in this example? or maybe you've set it somewhere else in your code?

also may want to check out php's ctype functions here.

  • Yes $taxonomy is being passed into the function, sorry I left that off. I also left out the length param in the substr function. Its working now. But I need to figure out how to get the foreign language characters to display. Thanx – dkmojo Jul 23 '11 at 16:18
  • @dkmojo - check out ctype linked above, specifically ctype_alpha, i believe you can set locale and check if a character is in various alphabets – Milo Jul 23 '11 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.