Created
August 11, 2020 10:39
-
-
Save bagerathan/6bb22387a495ea88847a9fe27595e1e3 to your computer and use it in GitHub Desktop.
Group taxonomy terms alphabetically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Snippet Name: List taxonomies by initial letter | |
* Snippet URL: http://www.wpcustoms.net/snippets/list-taxonomies-by-initial-letter/ | |
*/ | |
$list = ''; | |
$args = array( | |
'hide_empty' => true, | |
); | |
$tags = get_terms('CUSTOM-TAXONOMY',$args); | |
$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_line = ''; | |
foreach( $groups as $letter => $tags ) { | |
$list .= '<ul><li><a href="#"><strong>' . apply_filters( 'the_title', $letter ) . '</strong></a></li>'; | |
foreach( $tags as $tag ) { | |
$name = apply_filters( 'the_title', $tag->name ); | |
$list .= '<li><a href="' . get_term_link( $tag ) . '" title="' . sprintf(__('View all posts tagged with %s', 'yourtheme'), $tag->name) . '">' . $tag->name . ' <small>(' . $tag->count . ')</small></a></li>'; | |
} | |
$list .= '</ul><hr>'; | |
} | |
$list .= ''; | |
} | |
} else $list .= '<p>Sorry, but no tags were found</p>'; | |
print ($index_line); | |
print ($list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment