Advertisement
Viruthagiri

Untitled

Feb 12th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Template Name: Tag Index
  4. */
  5.  
  6. get_header();
  7.  
  8. $valid_characters = range( 'a' , 'z' );
  9. $valid_numbers = array(1,2,3,4,5,6,7,8,9,0);
  10.  
  11. // -----> This fixes the previous issue <----- //
  12. $ent = ( strpos( get_permalink( $post->ID ) , '?' ) != false ) ? '&' : '?' ;
  13.  
  14. $nav = array();
  15. // Build nav array
  16. foreach( $valid_characters as $key => $character ) {
  17.     $nav[] = '<a href="' . get_permalink( $post->ID ) . $ent.'tags=' . $character . '">' . strtoupper( $character ) . '</a>';
  18. }
  19. foreach( $valid_numbers as $key => $number ) {
  20.     $nav[] = '<a href="' . get_permalink( $post->ID ) . $ent.'tags=' . $number . '">' . $number . '</a>';
  21. }
  22. // 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)
  23. $tags = array();
  24. // Array that will hold arrays of tags based on their first letter
  25. $tag_array = array();
  26.  
  27. // Main switch
  28. switch( true ) {
  29.     // If request for tag with particular letter
  30.     case ( isset( $_GET['tags'] ) ):
  31.         // If it's in the arrays created earlier, get tafs and sort into array
  32.         case ( in_array( $_GET['tags'] , $valid_characters ) || in_array( $_GET['tags'] , $valid_numbers ) ):
  33.             $tags = get_terms( 'post_tag' , "hide_empty=0&name__like=$_GET[tags]" );
  34.  
  35.             if( !empty( $tags ) ) {
  36.                 foreach( $tags as $tag ) {
  37.                     $tag_array[$tag->name{0}][] = '<li><a href="'.get_tag_link( $tag->term_id ).'">'.$tag->name.'</a></li>';
  38.                 }
  39.             }
  40.         break;
  41.     break;
  42.     case ( !isset( $_GET['tags'] ) ):
  43.     default:
  44.         $tags = get_terms( 'post_tag' , 'hide_empty=0' );
  45.  
  46.         if( !empty( $tags ) ) {
  47.             foreach( $tags as $tag ) {
  48.                 $tag_array[$tag->name{0}][] = '<li><a href="'.get_tag_link( $tag->term_id ).'">'.$tag->name.'</a></li>';
  49.             }
  50.         }
  51.     break;
  52. }
  53. // 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
  54. if( count( $tag_array ) == 1 ) array_unshift( $nav , '<a href="' . get_permalink( $post->ID ) . '">Show all</a>' );
  55. ?>
  56.  
  57. <div id="content" class="widecolumn">
  58.     <!-- Feel free to add regular stuff you want here, like the_content() etc. -->
  59.     <div class="nav">
  60.         <?php
  61.             // Imploding an array is an easy and reliable way to create a string with seperators
  62.             echo implode( ' | ' , $nav );
  63.         ?>
  64.     </div>
  65. <?php
  66.     // If there's tags in the array
  67.     if( !empty( $tag_array ) ) {
  68.         foreach( $tag_array as $character => $character_tags ) {
  69.         ?>
  70.             <div class="tagindex">
  71.                 <h4><?php echo $character; ?></h4>
  72.                 <ul class="links">
  73.                     <?php foreach( $character_tags as $key => $tag ) { echo $tag; } ?>
  74.                 </ul>
  75.             </div>
  76.         <?php
  77.         }
  78.     }
  79.     else {
  80.         ?>
  81.         <h4>No tags found</h4>
  82.         <?php
  83.     }
  84.     ?>
  85. </div>
  86. </div>
  87. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement