Advertisement
vtxyzzy

A-Z Pages, No A, An, The

Jan 24th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.61 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: A-Z Pages Word 2
  4.  
  5. A WordPress template to list page titles by first letter except that it
  6. ignores leading 'A', 'An', and 'The'.
  7.  
  8. You should modify the CSS to suit your theme and place it in its proper file.
  9. Be sure to set the $posts_per_row, $posts_per_page, and $post_type variables.
  10. */
  11. $posts_per_row = 3;
  12. $posts_per_page = 15;
  13. $post_type = 'post';
  14. ?>
  15.  
  16. <?php
  17.    /* These functions should go in your functions.php file */
  18.   /*
  19.  
  20.   function mam_posts_fields ($fields) {
  21.    global $mam_global_fields;
  22.    // Make sure there is a leading comma
  23.    if ($mam_global_fields) $fields .= (preg_match('/^(\s+)?,/',$mam_global_fields)) ? $mam_global_fields : ", $mam_global_fields";
  24.    return $fields;
  25. }
  26. function mam_posts_orderby ($orderby) {
  27.    global $mam_global_orderby;
  28.    if ($mam_global_orderby) $orderby = $mam_global_orderby;
  29.    return $orderby;
  30. }
  31. add_filter('posts_fields','mam_posts_fields');
  32. add_filter('posts_orderby','mam_posts_orderby');
  33.  
  34.   */
  35. ?>
  36.  
  37. <?php get_header(); ?>
  38.  
  39. <style type="text/css">
  40. .letter-group { width: 100%; }
  41. .letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
  42. .row-cells { width: 70%; float: right; margin-right: 180px; }
  43. .title-cell { width: 50%;  float: left; overflow: hidden; margin-bottom: 8px; }
  44. .clear { clear: both; }
  45. </style>
  46.  
  47. <div id="main-background">
  48.  
  49.    <div id="main-column">
  50.       <h1><?php the_title(); ?></h1>
  51.  
  52.       <div class="margin-top"></div>
  53.  
  54.       <div id="a-z">
  55.  
  56.          <?php
  57.          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  58.          $args = array (
  59.             'posts_per_page' => $posts_per_page,
  60.             'post_type' => $post_type,
  61.             'orderby' => 'title',
  62.             'order' => 'ASC',
  63.             'paged' => $paged
  64.          );
  65.          global $mam_global_fields, $mam_global_orderby;
  66.          $mam_global_fields = ",
  67.            IF($wpdb->posts.post_title REGEXP('^the '),CONCAT(SUBSTR($wpdb->posts.post_title,5), ', ', SUBSTR($wpdb->posts.post_title,1,4)),
  68.               IF($wpdb->posts.post_title REGEXP('^an '),CONCAT(SUBSTR($wpdb->posts.post_title,4),', ', SUBSTR($wpdb->posts.post_title,1,3)),
  69.                  IF($wpdb->posts.post_title REGEXP('^a '),CONCAT(SUBSTR($wpdb->posts.post_title,3),', ', SUBSTR($wpdb->posts.post_title,1,2)),
  70.                     $wpdb->posts.post_title) )  )  AS sort_title";
  71.          $mam_global_orderby = " UPPER(sort_title) ASC";
  72.          query_posts($args);
  73.          // print_r($wp_query->request);
  74.          $mam_global_fields = $mam_global_orderby = '';
  75.          if ( have_posts() ) {
  76.             $in_this_row = 0;
  77.             while ( have_posts() ) {
  78.                the_post();
  79.                $first_letter = strtoupper(substr(apply_filters('the_title',$post->sort_title),0,1));
  80.                if ($first_letter != $curr_letter) {
  81.                   if (++$post_count > 1) {
  82.                      end_prev_letter();
  83.                   }
  84.                   start_new_letter($first_letter);
  85.                   $curr_letter = $first_letter;
  86.                }
  87.                if (++$in_this_row > $posts_per_row) {
  88.                   end_prev_row();
  89.                   start_new_row();
  90.                   ++$in_this_row;  // Account for this first post
  91.                } ?>
  92.                <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo apply_filters('the_title',$post->sort_title); ?></a></div>
  93.             <?php }
  94.             end_prev_letter();
  95.             ?>
  96.             <div class="navigation">
  97.                <div class="alignleft"><?php next_posts_link('&laquo; Higher Letters') ?></div>
  98.                <div class="alignright"><?php previous_posts_link('Lower Letters &raquo;') ?></div>
  99.             </div>
  100.          <?php } else {
  101.             echo "<h2>Sorry, no posts were found!</h2>";
  102.          }
  103.          ?>
  104.  
  105.       </div><!-- End id='a-z' -->
  106.  
  107.    </div><!-- End class='margin-top -->
  108.  
  109. </div><!-- End id='rightcolumn' -->
  110.  
  111. <?php get_sidebar(); ?>
  112. <?php get_footer(); ?>
  113.  
  114. <?php
  115. function end_prev_letter() {
  116.    end_prev_row();
  117.    echo "</div><!-- End of letter-group -->\n";
  118.    echo "<div class='clear'></div>\n";
  119. }
  120. function start_new_letter($letter) {
  121.    echo "<div class='letter-group'>\n";
  122.    echo "\t<div class='letter-cell'>$letter</div>\n";
  123.    start_new_row($letter);
  124. }
  125. function end_prev_row() {
  126.    echo "\t</div><!-- End row-cells -->\n";
  127. }
  128. function start_new_row() {
  129.    global $in_this_row;
  130.    $in_this_row = 0;
  131.    echo "\t<div class='row-cells'>\n";
  132. }
  133.  
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement