How to list taxonomy terms by first letter?
-
Hi guys,
I have a site selling sports equipment. I have a taxonomy named ‘Brands’, and in that taxonomy terms such as nike, adidas etc. Im trying to create a page for my website of brands listed a-z. The list will simply be the taxonomy term, and i don’t want these terms to be links, purely a list for reference purposes.
I don’t have a brand representing every letter of the alphabet, so the code needs to check if there are any brands and then display the name accordingly. Does anyone know how to do this??
I found this on a similar post that works but relates to tags. If anyone can amend the code so it works for the taxonomy terms i would be very grateful. Thanks, Patrick.
<?php $list = ''; $tags = get_terms('your_custom_taxonomy'); $groups = array(); if( $tags && is_array( $tags ) ) { foreach( $tags as $tag ) { $first_letter = strtoupper( $tag->name[0] ); $groups[ $first_letter ][] = $tag; } if( !empty( $groups ) ) { { $index_row .='<ul class="topindex">'; foreach ($groups as $letter => $tags) { $index_row .= ' <li><h4><a rel="nofollow" href="#' . $letter . '-folder" title="' . $letter . ' folder">' . apply_filters( 'the_title', $letter ) . '</a></h4></li> '; } $index_row .='<br class="clear">'; } $list .= '<ul class="index">'; foreach( $groups as $letter => $tags ) { $list .= ' <li><a></a><h5><a rel="nofollow" href="#tags_top" title="Back to index">' . apply_filters( 'the_title', $letter ) . '</a></h5>'; $list .= '<ul class="links">'; foreach( $tags as $tag ) { $url = attribute_escape(get_bloginfo('url') . '/' . $tag->taxonomy . '/' . $tag->slug); $name = apply_filters( 'the_title', $tag->name ); $list .= '<li class="cat-item"><a rel="nofollow" title="' . $name . ' folder" href="' . $custom_taxonomy_parent . $url . '">' . $name . '</a></li> '; } $list .= ''; } $list .= ''; } } else $list .= '<p>Sorry, but no tags were found</p>'; ?> <a></a> <?php print $index_row; ?> <?php print $list; ?>
- The topic ‘How to list taxonomy terms by first letter?’ is closed to new replies.