Here’s the code to display categories alphabetically in a list with clickable anchor links:
<?php
$list = '';
$cats = get_terms('category');
$groups = array();
if( $cats && is_array( $cats ) ) {
foreach( $cats as $cat ) {
$first_letter = strtoupper( $cat->name[0] );
$groups[ $first_letter ][] = $cat;
}
if( !empty( $groups ) ) {{
$index_row .='<ul class="topindex">';
foreach ($groups as $letter => $cats) {
$index_row .= '<li><h4><a href="#' . $letter . '" title="' . $letter . '">' . apply_filters( 'the_title', $letter ) . '</a></h4></li>';
}
$index_row .='</ul><br class="clear" />';}
$list .= '<ul class="index">';
foreach( $groups as $letter => $cats ) {
$list .= '<li><a name="' . $letter . '"></a><h5><a href="#cats_top" title="back to top">' . apply_filters( 'the_title', $letter ) . '</a></h5>';
$list .= '<ul class="links">';
foreach( $cats as $cat ) {
$url = attribute_escape( get_category_link( $cat->term_id ) );
$name = apply_filters( 'the_title', $cat->name );
$list .= '<li><a title="' . $name . '" href="' . $url . '">' . $name . '</a></li>';
} $list .= '</ul></li>';
} $list .= '</ul>';
}
}else $list .= '<p>Sorry, but no cats were found</p>';
?>
<a name="cats_top"></a>
<?php print $index_row; ?>
<?php print $list; ?>