Skip to content

Instantly share code, notes, and snippets.

@calliaweb
Created July 1, 2015 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calliaweb/f373e8eb32c3388175b4 to your computer and use it in GitHub Desktop.
Save calliaweb/f373e8eb32c3388175b4 to your computer and use it in GitHub Desktop.
<?php
/**
* Display alphabetical list of tags.
* Template Name: Tag Index
* @author Joanne Waltham
*/
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'jmw_do_tag_index_loop' );
function jmw_do_tag_index_loop() {
//number of columns
$cols = 3;
//Get array of tags
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => TRUE,
);
//Get list of tags, if none then quit
if ( ! $tags = get_tags( $args ) )
return;
echo '<div class="tag-index"><h1 class="entry-title">' . get_the_title() .'</h1>';
//Set up for first time through loop
$prev_letter = strtoupper( $tags[0]->name[0] );
//Loop through tags to find out letters in use and number of tags with that letter
foreach ( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
if ( $first_letter != $prev_letter ) {
$alphabet[ $prev_letter ] = $tag_count;
$tag_count = 0;
}
$tag_count = $tag_count + 1;
$prev_letter = $first_letter;
}
//Dont forget last letter in tag list
$alphabet[ $prev_letter ] = $tag_count;
//Create Navigation
$letter_nav = array_keys( $alphabet );
echo '<div class="tag-nav">';
foreach ( $letter_nav as $letter ) {
echo '<a href="#tag-' . $letter . '">' . $letter . ' </a>';
}
echo '</div>';
//Set up for first time through loop
$prev_initial = strtoupper( $tags[0]->name[0] );
echo '<div class="index-group"><h2 id="tag-' .$prev_initial . '">' . $prev_initial . '</h2>';
$col_num = ceil( $alphabet[ $prev_initial ] / $cols );
$this_count = 0;
echo '<div class="one-third first"><ul>';
//Loop Through Tags
foreach ( $tags as $tag ) {
//Find out first letter of tag
$current_initial = strtoupper( $tag->name[0] );
//If its the first time of that letter create a heading and new section
if ( $current_initial != $prev_initial ) {
echo '</ul></div></div><div class="index-group"><h2 id="tag-' .$current_initial . '">' . $current_initial . '</h2>';
//How many to one column
$col_num = ceil( $alphabet[ $current_initial ] / $cols );
$this_count = 0;
echo '<div class="one-third first"><ul>';
$col_count = 1;
}
//Output list of tags
$this_count = $this_count + 1;
echo '<li><a href="' . get_tag_link($tag->term_id) .'">' . $tag->name . '</a>&nbsp;(' . $tag->count . ')</li>';
if ( 0 == $this_count % $col_num && $col_count < $cols ) {
echo '</ul></div><div class="one-third"><ul>';
$col_count = $col_count + 1;
}
$prev_initial = $current_initial;
}
echo '</ul></div></div></div>';
}
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment