SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | /* | |
3 | Template Name: A-Z Pages by CF Letter | |
4 | ||
5 | A WordPress template to list page titles by first letter of a Custom Field. | |
6 | ||
7 | You should modify the CSS to suit your theme and place it in its proper file. | |
8 | Be sure to set the $meta_key, $posts_per_row and $posts_per_page variables. | |
9 | */ | |
10 | ||
11 | // These functions should go in functions.php | |
12 | ||
13 | function mam_posts_join ($join) { | |
14 | global $mam_global_join; | |
15 | if ($mam_global_join) $join .= " $mam_global_join"; | |
16 | return $join; | |
17 | } | |
18 | add_filter('posts_join','mam_posts_join'); | |
19 | ||
20 | function mam_posts_where ($where) { | |
21 | global $mam_global_where; | |
22 | if ($mam_global_where) $where .= " $mam_global_where"; | |
23 | return $where; | |
24 | } | |
25 | add_filter('posts_where','mam_posts_where'); | |
26 | ||
27 | $meta_key = 'x'; | |
28 | $posts_per_row = 3; | |
29 | $posts_per_page = -1; | |
30 | $pageURL = 'http'; | |
31 | $post_type = 'post'; | |
32 | ||
33 | if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} | |
34 | $pageURL .= "://"; | |
35 | if ($_SERVER["SERVER_PORT"] != "80") { | |
36 | $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; | |
37 | } else { | |
38 | $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; | |
39 | } | |
40 | ||
41 | $letters = $wpdb->get_col( | |
42 | "SELECT DISTINCT LEFT(m.meta_value,1) AS first_letter FROM $wpdb->posts p | |
43 | JOIN $wpdb->postmeta m ON (m.post_id = p.ID) | |
44 | WHERE post_type = '$post_type' AND post_status = 'publish' | |
45 | AND m.meta_key = '$meta_key' | |
46 | ORDER BY first_letter ASC" | |
47 | ); | |
48 | ||
49 | $first_letter = ($_GET['first_letter']) ? $_GET['first_letter'] : $letters[0]; | |
50 | ?> | |
51 | ||
52 | <?php get_header(); ?> | |
53 | ||
54 | <style type="text/css"> | |
55 | .letter-group { width: 100%; } | |
56 | .letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; } | |
57 | .row-cells { width: 70%; float: right; margin-right: 180px; } | |
58 | .title-cell { width: 30%; float: left; overflow: hidden; margin-bottom: 8px; } | |
59 | .clear { clear: both; } | |
60 | </style> | |
61 | ||
62 | <div id="main-background"> | |
63 | ||
64 | <div id="main-column"> | |
65 | <h1><?php the_title(); ?></h1> | |
66 | ||
67 | <div class="margin-top"></div> | |
68 | ||
69 | <div id="a-z"> | |
70 | ||
71 | <?php | |
72 | $mam_global_where = " AND m.meta_key ='$meta_key' AND LEFT(m.meta_value,1) = '$first_letter' "; | |
73 | $mam_global_join = " JOIN $wpdb->postmeta m ON (m.post_id = ID) "; | |
74 | $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
75 | $args = array ( | |
76 | 'posts_per_page' => $posts_per_page, | |
77 | 'post_type' => $post_type, | |
78 | 'caller_get_posts' => 1, | |
79 | 'orderby' => 'title', | |
80 | 'order' => 'ASC', | |
81 | 'paged' => $paged, | |
82 | ); | |
83 | query_posts($args); | |
84 | ||
85 | $mam_global_where = ''; // Turn off filter | |
86 | if ( have_posts() ) { | |
87 | $in_this_row = 0; | |
88 | while ( have_posts() ) { | |
89 | the_post(); | |
90 | $first_letter = strtoupper(substr($post->meta_value,0,1)); | |
91 | if ($first_letter != $curr_letter) { | |
92 | if (++$post_count > 1) { | |
93 | end_prev_letter(); | |
94 | } | |
95 | start_new_letter($first_letter); | |
96 | $curr_letter = $first_letter; | |
97 | } | |
98 | if (++$in_this_row > $posts_per_row) { | |
99 | end_prev_row(); | |
100 | start_new_row(); | |
101 | ++$in_this_row; // Account for this first post | |
102 | } ?> | |
103 | <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div> | |
104 | <?php } | |
105 | end_prev_letter(); | |
106 | ?> | |
107 | <div class="navigation"> | |
108 | <?php | |
109 | foreach ($letters as $letter) { | |
110 | $url = add_query_arg('first_letter',$letter,$pageURL); | |
111 | echo "<a href='$url' title='Starting letter $letter' >[ $letter ] </a>"; | |
112 | } | |
113 | ?> | |
114 | </div> | |
115 | <?php } else { | |
116 | echo "<h2>Sorry, no posts were found!</h2>"; | |
117 | } | |
118 | ?> | |
119 | ||
120 | </div><!-- End id='a-z' --> | |
121 | ||
122 | </div><!-- End class='margin-top --> | |
123 | ||
124 | </div><!-- End id='rightcolumn' --> | |
125 | ||
126 | <?php get_sidebar(); ?> | |
127 | <?php get_footer(); ?> | |
128 | ||
129 | <?php | |
130 | function end_prev_letter() { | |
131 | end_prev_row(); | |
132 | echo "</div><!-- End of letter-group -->\n"; | |
133 | echo "<div class='clear'></div>\n"; | |
134 | } | |
135 | function start_new_letter($letter) { | |
136 | echo "<div class='letter-group'>\n"; | |
137 | echo "\t<div class='letter-cell'>$letter</div>\n"; | |
138 | start_new_row($letter); | |
139 | } | |
140 | function end_prev_row() { | |
141 | echo "\t</div><!-- End row-cells -->\n"; | |
142 | } | |
143 | function start_new_row() { | |
144 | global $in_this_row; | |
145 | $in_this_row = 0; | |
146 | echo "\t<div class='row-cells'>\n"; | |
147 | } | |
148 | ||
149 | ?> |