Advertisement
vtxyzzy

List Posts in Letter Groups

Jun 2nd, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.32 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: A-Z Pages by Letter GROUPS
  4.  
  5. A WordPress template to list page titles in groups by first letter.
  6.  
  7. Created for the Twenty Ten theme.
  8.  
  9. It currently does not include paging, so $posts_per_page should be -1.
  10.  
  11. Be sure to set the $post_type and $posts_per_row variables.
  12. Note that the .title_cell width must agree with the $posts_per_row.
  13. That is, if $posts_per_row = 2, the width should be 50%.
  14.  
  15. You should modify the div structure and CSS to suit your theme and place
  16. the CSS in its proper file.
  17.  
  18. */
  19.  
  20. // This function should go in functions.php
  21. //function mam_posts_where ($where) {
  22. //   global $mam_global_where;
  23. //   if ($mam_global_where) $where .= " $mam_global_where";
  24. //   return $where;
  25. //}
  26. //add_filter('posts_where','mam_posts_where');
  27.  
  28. $posts_per_row = 1;
  29. $posts_per_page = -1;
  30. $pageURL = 'http';
  31. $post_type = 'page';
  32. $grp_hi_letters = array('E','I','M','Q','U','~');  // "x'FF'" is for MySQL hex FF
  33. $grp_labels = array('A-E', 'F-I','J-M','N-Q','R-U','V-Z');
  34.  
  35. if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  36. $pageURL .= "://";
  37. if ($_SERVER["SERVER_PORT"] != "80") {
  38.  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  39. } else {
  40.  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  41. }
  42.  
  43. $letters = $wpdb->get_col(
  44. "SELECT DISTINCT UPPER(LEFT(post_title,1)) AS first_letter FROM $wpdb->posts
  45. WHERE post_type = '$post_type' AND post_status = 'publish'
  46. ORDER BY first_letter ASC"
  47. );
  48.  
  49. $letter_grp = (in_array($_GET['letter_grp'], $grp_labels)) ? $_GET['letter_grp'] : $grp_labels[0];
  50. $letter_grp_ndx = array_search($letter_grp, $grp_labels);
  51. $letter_grp = urldecode($letter_grp);
  52. ?>
  53.  
  54. <?php get_header(); ?>
  55.  
  56. <style type="text/css">
  57. #a-z { width: 710px; float: left; }
  58. .letter-group { width: 100%; }
  59. .letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
  60. .row-cells { width: 100%; margin-left: 10px;w }
  61. .title-cell { width: 100%;  float: left; overflow: hidden; margin-bottom: 8px; }
  62. .clear { clear: both; }
  63. </style>
  64.  
  65. <div id="main-background">
  66.  
  67.    <div id="main-column">
  68.       <h1><?php the_title(); ?></h1>
  69.  
  70.       <div class="margin-top"></div>
  71.  
  72.       <div id="a-z">
  73.  
  74.          <?php
  75.          // echo "<p>LETTER_GRP:" . htmlentities($letter_grp) . '</p>';
  76.          $hi_letter = $grp_hi_letters[$letter_grp_ndx];
  77.          $lo_letter = ($letter_grp_ndx > 0) ? $grp_hi_letters[$letter_grp_ndx - 1] : ' ';
  78.          $mam_global_where = " AND UPPER(LEFT(post_title,1)) > '$lo_letter' AND UPPER(LEFT(post_title,1)) <= '$hi_letter' ";
  79.          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  80.          $args = array (
  81.             'posts_per_page' => $posts_per_page,
  82.             'post_type' => $post_type,
  83.             'orderby' => 'title',
  84.             'order' => 'ASC',
  85.             'paged' => $paged
  86.          );
  87.          query_posts($args);
  88.          // global $wp_query; print_r('<p>QUERY:');print_r($wp_query->request);print_r('</p>');
  89.          $mam_global_where = '';  // Turn off filter
  90.          ?>
  91.          <div class="navigation">
  92.          <?php
  93.          $curr_grp = '';
  94.          $curr_ndx = 0;
  95.          foreach ($letters as $letter) {
  96.             while ($letter > $grp_hi_letters[$curr_ndx]) {
  97.                ++$curr_ndx;
  98.             }
  99.             $this_grp = $grp_labels[$curr_ndx];
  100.             if ($curr_grp != $this_grp) {
  101.                $curr_grp = $this_grp;
  102.                $url = add_query_arg('letter_grp',urlencode($this_grp),$pageURL);
  103.                echo "<a href='$url' title='Letter group $letter' >[ $this_grp ]&nbsp;&nbsp;</a>";
  104.             }
  105.          }
  106.          ?>
  107.          </div>
  108.          <?php
  109.          if ( have_posts() ) {
  110.             $in_this_row = 0;
  111.             $curr_grp = '';
  112.             $curr_ndx = 0;
  113.             while ( have_posts() ) {
  114.                the_post();
  115.                $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
  116.                while ($first_letter > $grp_hi_letters[$curr_ndx]) {
  117.                   ++$curr_ndx;
  118.                }
  119.                $this_grp = $grp_labels[$curr_ndx];
  120.                if ($this_grp != $curr_grp) {
  121.                   if (++$post_count > 1) {
  122.                      end_prev_letter();
  123.                   }
  124.                   start_new_letter($this_grp);
  125.                   $curr_letter = $first_letter;
  126.                   $curr_grp = $this_grp;
  127.                }
  128.                if (++$in_this_row > $posts_per_row) {
  129.                   end_prev_row();
  130.                   start_new_row();
  131.                   ++$in_this_row;  // Account for this first post
  132.                } ?>
  133.                <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
  134.             <?php }
  135.             end_prev_letter();
  136.             ?>
  137.             <div class="navigation">
  138.             <?php
  139.             $curr_grp = '';
  140.             $curr_ndx = 0;
  141.             foreach ($letters as $letter) {
  142.                while ($letter > $grp_hi_letters[$curr_ndx]) {
  143.                   ++$curr_ndx;
  144.                }
  145.                $this_grp = $grp_labels[$curr_ndx];
  146.                if ($curr_grp != $this_grp) {
  147.                   $curr_grp = $this_grp;
  148.                   $url = add_query_arg('letter_grp',urlencode($this_grp),$pageURL);
  149.                   echo "<a href='$url' title='Letter group $letter' >[ $this_grp ]&nbsp;&nbsp;</a>";
  150.                }
  151.             }
  152.             ?>
  153.             </div>
  154.          <?php } else {
  155.             echo "<h2>Sorry, no posts were found!</h2>";
  156.          }
  157.          ?>
  158.  
  159.       </div><!-- End id='a-z' -->
  160.  
  161.    </div><!-- End class='margin-top -->
  162.  
  163. </div><!-- End id='rightcolumn' -->
  164.  
  165. <?php get_sidebar(); ?>
  166. <?php get_footer(); ?>
  167.  
  168. <?php
  169. function end_prev_letter() {
  170.    end_prev_row();
  171.    echo "</div><!-- End of letter-group -->\n";
  172.    echo "<div class='clear'></div>\n";
  173. }
  174. function start_new_letter($letter) {
  175.    echo "<div class='letter-group'>\n";
  176.    echo "\t<div class='letter-cell'>$letter</div>\n";
  177.    start_new_row($letter);
  178. }
  179. function end_prev_row() {
  180.    echo "\t</div><!-- End row-cells -->\n";
  181. }
  182. function start_new_row() {
  183.    global $in_this_row;
  184.    $in_this_row = 0;
  185.    echo "\t<div class='row-cells'>\n";
  186. }
  187.  
  188. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement