SHARE
TWEET

Mac/WP List Posts by First Letter of Title

a guest Dec 2nd, 2010 2,017 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Template Name: A-Z Pages
  4.  
  5. A WordPress template to list page titles by first letter.
  6.  
  7. You should modify the CSS to suit your theme and place it in its proper file.
  8. Be sure to set the $posts_per_row and $posts_per_page variables.
  9. */
  10. $posts_per_row = 3;
  11. $posts_per_page = 15;
  12. ?>
  13.  
  14. <?php get_header(); ?>
  15.  
  16. <style type="text/css">
  17. .letter-group { width: 100%; }
  18. .letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
  19. .row-cells { width: 70%; float: right; margin-right: 180px; }
  20. .title-cell { width: 30%;  float: left; overflow: hidden; margin-bottom: 8px; }
  21. .clear { clear: both; }
  22. </style>
  23.  
  24. <div id="main-background">
  25.  
  26.    <div id="main-column">
  27.       <h1><?php the_title(); ?></h1>
  28.  
  29.       <div class="margin-top"></div>
  30.  
  31.       <div id="a-z">
  32.  
  33.          <?php
  34.          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  35.          $args = array (
  36.             'posts_per_page' => $posts_per_page,
  37.             'post_type' => 'page',
  38.             'orderby' => 'title',
  39.             'order' => 'ASC',
  40.             'paged' => $paged
  41.          );
  42.          query_posts($args);
  43.          if ( have_posts() ) {
  44.             $in_this_row = 0;
  45.             while ( have_posts() ) {
  46.                the_post();
  47.                $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
  48.                if ($first_letter != $curr_letter) {
  49.                   if (++$post_count > 1) {
  50.                      end_prev_letter();
  51.                   }
  52.                   start_new_letter($first_letter);
  53.                   $curr_letter = $first_letter;
  54.                }
  55.                if (++$in_this_row > $posts_per_row) {
  56.                   end_prev_row();
  57.                   start_new_row();
  58.                   ++$in_this_row;  // Account for this first post
  59.                } ?>
  60.                <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
  61.             <?php }
  62.             end_prev_letter();
  63.             ?>
  64.             <div class="navigation">
  65.                <div class="alignleft"><?php next_posts_link('&laquo; Higher Letters') ?></div>
  66.                <div class="alignright"><?php previous_posts_link('Lower Letters &raquo;') ?></div>
  67.             </div>
  68.          <?php } else {
  69.             echo "<h2>Sorry, no posts were found!</h2>";
  70.          }
  71.          ?>
  72.  
  73.       </div><!-- End id='a-z' -->
  74.  
  75.    </div><!-- End class='margin-top -->
  76.  
  77. </div><!-- End id='rightcolumn' -->
  78.  
  79. <?php get_sidebar(); ?>
  80. <?php get_footer(); ?>
  81.  
  82. <?php
  83. function end_prev_letter() {
  84.    end_prev_row();
  85.    echo "</div><!-- End of letter-group -->\n";
  86.    echo "<div class='clear'></div>\n";
  87. }
  88. function start_new_letter($letter) {
  89.    echo "<div class='letter-group'>\n";
  90.    echo "\t<div class='letter-cell'>$letter</div>\n";
  91.    start_new_row($letter);
  92. }
  93. function end_prev_row() {
  94.    echo "\t</div><!-- End row-cells -->\n";
  95. }
  96. function start_new_row() {
  97.    global $in_this_row;
  98.    $in_this_row = 0;
  99.    echo "\t<div class='row-cells'>\n";
  100. }
  101.  
  102. ?>
RAW Paste Data
Top