Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Template Name: Tag Index
- */
- get_header();
- $valid_characters = range( 'a' , 'z' );
- $valid_numbers = array(1,2,3,4,5,6,7,8,9,0);
- // -----> This fixes the previous issue <----- //
- $ent = ( strpos( get_permalink( $post->ID ) , '?' ) != false ) ? '&' : '?' ;
- $nav = array();
- // Build nav array
- foreach( $valid_characters as $key => $character ) {
- $nav[] = '<a href="' . get_permalink( $post->ID ) . $ent.'tags=' . $character . '">' . strtoupper( $character ) . '</a>';
- }
- foreach( $valid_numbers as $key => $number ) {
- $nav[] = '<a href="' . get_permalink( $post->ID ) . $ent.'tags=' . $number . '">' . $number . '</a>';
- }
- // Array to hold tags, needs to be set, so the empty check works when none of the switch cases hit a match (for whatever reason)
- $tags = array();
- // Array that will hold arrays of tags based on their first letter
- $tag_array = array();
- // Main switch
- switch( true ) {
- // If request for tag with particular letter
- case ( isset( $_GET['tags'] ) ):
- // If it's in the arrays created earlier, get tafs and sort into array
- case ( in_array( $_GET['tags'] , $valid_characters ) || in_array( $_GET['tags'] , $valid_numbers ) ):
- $tags = get_terms( 'post_tag' , "hide_empty=0&name__like=$_GET[tags]" );
- if( !empty( $tags ) ) {
- foreach( $tags as $tag ) {
- $tag_array[$tag->name{0}][] = '<li><a href="'.get_tag_link( $tag->term_id ).'">'.$tag->name.'</a></li>';
- }
- }
- break;
- break;
- case ( !isset( $_GET['tags'] ) ):
- default:
- $tags = get_terms( 'post_tag' , 'hide_empty=0' );
- if( !empty( $tags ) ) {
- foreach( $tags as $tag ) {
- $tag_array[$tag->name{0}][] = '<li><a href="'.get_tag_link( $tag->term_id ).'">'.$tag->name.'</a></li>';
- }
- }
- break;
- }
- // If theres only one item in the array ( well one key ) , then it's a requested tag letter, so add a "Show all" link at the start of the nav
- if( count( $tag_array ) == 1 ) array_unshift( $nav , '<a href="' . get_permalink( $post->ID ) . '">Show all</a>' );
- ?>
- <div id="content" class="widecolumn">
- <!-- Feel free to add regular stuff you want here, like the_content() etc. -->
- <div class="nav">
- <?php
- // Imploding an array is an easy and reliable way to create a string with seperators
- echo implode( ' | ' , $nav );
- ?>
- </div>
- <?php
- // If there's tags in the array
- if( !empty( $tag_array ) ) {
- foreach( $tag_array as $character => $character_tags ) {
- ?>
- <div class="tagindex">
- <h4><?php echo $character; ?></h4>
- <ul class="links">
- <?php foreach( $character_tags as $key => $tag ) { echo $tag; } ?>
- </ul>
- </div>
- <?php
- }
- }
- else {
- ?>
- <h4>No tags found</h4>
- <?php
- }
- ?>
- </div>
- </div>
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement