Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Template Name: A-Z Pages by Letter GROUPS
- A WordPress template to list page titles in groups by first letter.
- Created for the Twenty Ten theme.
- It currently does not include paging, so $posts_per_page should be -1.
- Be sure to set the $post_type and $posts_per_row variables.
- Note that the .title_cell width must agree with the $posts_per_row.
- That is, if $posts_per_row = 2, the width should be 50%.
- You should modify the div structure and CSS to suit your theme and place
- the CSS in its proper file.
- */
- // This function should go in functions.php
- //function mam_posts_where ($where) {
- // global $mam_global_where;
- // if ($mam_global_where) $where .= " $mam_global_where";
- // return $where;
- //}
- //add_filter('posts_where','mam_posts_where');
- $posts_per_row = 1;
- $posts_per_page = -1;
- $pageURL = 'http';
- $post_type = 'page';
- $grp_hi_letters = array('E','I','M','Q','U','~'); // "x'FF'" is for MySQL hex FF
- $grp_labels = array('A-E', 'F-I','J-M','N-Q','R-U','V-Z');
- if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
- $pageURL .= "://";
- if ($_SERVER["SERVER_PORT"] != "80") {
- $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- } else {
- $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
- }
- $letters = $wpdb->get_col(
- "SELECT DISTINCT UPPER(LEFT(post_title,1)) AS first_letter FROM $wpdb->posts
- WHERE post_type = '$post_type' AND post_status = 'publish'
- ORDER BY first_letter ASC"
- );
- $letter_grp = (in_array($_GET['letter_grp'], $grp_labels)) ? $_GET['letter_grp'] : $grp_labels[0];
- $letter_grp_ndx = array_search($letter_grp, $grp_labels);
- $letter_grp = urldecode($letter_grp);
- ?>
- <?php get_header(); ?>
- <style type="text/css">
- #a-z { width: 710px; float: left; }
- .letter-group { width: 100%; }
- .letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
- .row-cells { width: 100%; margin-left: 10px;w }
- .title-cell { width: 100%; float: left; overflow: hidden; margin-bottom: 8px; }
- .clear { clear: both; }
- </style>
- <div id="main-background">
- <div id="main-column">
- <h1><?php the_title(); ?></h1>
- <div class="margin-top"></div>
- <div id="a-z">
- <?php
- // echo "<p>LETTER_GRP:" . htmlentities($letter_grp) . '</p>';
- $hi_letter = $grp_hi_letters[$letter_grp_ndx];
- $lo_letter = ($letter_grp_ndx > 0) ? $grp_hi_letters[$letter_grp_ndx - 1] : ' ';
- $mam_global_where = " AND UPPER(LEFT(post_title,1)) > '$lo_letter' AND UPPER(LEFT(post_title,1)) <= '$hi_letter' ";
- $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
- $args = array (
- 'posts_per_page' => $posts_per_page,
- 'post_type' => $post_type,
- 'orderby' => 'title',
- 'order' => 'ASC',
- 'paged' => $paged
- );
- query_posts($args);
- // global $wp_query; print_r('<p>QUERY:');print_r($wp_query->request);print_r('</p>');
- $mam_global_where = ''; // Turn off filter
- ?>
- <div class="navigation">
- <?php
- $curr_grp = '';
- $curr_ndx = 0;
- foreach ($letters as $letter) {
- while ($letter > $grp_hi_letters[$curr_ndx]) {
- ++$curr_ndx;
- }
- $this_grp = $grp_labels[$curr_ndx];
- if ($curr_grp != $this_grp) {
- $curr_grp = $this_grp;
- $url = add_query_arg('letter_grp',urlencode($this_grp),$pageURL);
- echo "<a href='$url' title='Letter group $letter' >[ $this_grp ] </a>";
- }
- }
- ?>
- </div>
- <?php
- if ( have_posts() ) {
- $in_this_row = 0;
- $curr_grp = '';
- $curr_ndx = 0;
- while ( have_posts() ) {
- the_post();
- $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
- while ($first_letter > $grp_hi_letters[$curr_ndx]) {
- ++$curr_ndx;
- }
- $this_grp = $grp_labels[$curr_ndx];
- if ($this_grp != $curr_grp) {
- if (++$post_count > 1) {
- end_prev_letter();
- }
- start_new_letter($this_grp);
- $curr_letter = $first_letter;
- $curr_grp = $this_grp;
- }
- if (++$in_this_row > $posts_per_row) {
- end_prev_row();
- start_new_row();
- ++$in_this_row; // Account for this first post
- } ?>
- <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
- <?php }
- end_prev_letter();
- ?>
- <div class="navigation">
- <?php
- $curr_grp = '';
- $curr_ndx = 0;
- foreach ($letters as $letter) {
- while ($letter > $grp_hi_letters[$curr_ndx]) {
- ++$curr_ndx;
- }
- $this_grp = $grp_labels[$curr_ndx];
- if ($curr_grp != $this_grp) {
- $curr_grp = $this_grp;
- $url = add_query_arg('letter_grp',urlencode($this_grp),$pageURL);
- echo "<a href='$url' title='Letter group $letter' >[ $this_grp ] </a>";
- }
- }
- ?>
- </div>
- <?php } else {
- echo "<h2>Sorry, no posts were found!</h2>";
- }
- ?>
- </div><!-- End id='a-z' -->
- </div><!-- End class='margin-top -->
- </div><!-- End id='rightcolumn' -->
- <?php get_sidebar(); ?>
- <?php get_footer(); ?>
- <?php
- function end_prev_letter() {
- end_prev_row();
- echo "</div><!-- End of letter-group -->\n";
- echo "<div class='clear'></div>\n";
- }
- function start_new_letter($letter) {
- echo "<div class='letter-group'>\n";
- echo "\t<div class='letter-cell'>$letter</div>\n";
- start_new_row($letter);
- }
- function end_prev_row() {
- echo "\t</div><!-- End row-cells -->\n";
- }
- function start_new_row() {
- global $in_this_row;
- $in_this_row = 0;
- echo "\t<div class='row-cells'>\n";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement