Created
January 18, 2012 13:13
-
-
Save pdewouters/1632959 to your computer and use it in GitHub Desktop.
glossary template, lists posts alphabetiaclly and displays an alphabetical nav bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The glossary archive template | |
*/ | |
/** | |
* Remove the standard loop | |
*/ | |
remove_action( 'genesis_loop', 'genesis_do_loop' ); | |
add_action( 'genesis_loop', 'aha_glossary_loop' ); | |
/** | |
* Custom loop for glossary archive page | |
*/ | |
function aha_glossary_loop() { | |
//create navigation | |
// get posts array | |
$glossary_posts = get_posts(array('posts_per_page' => '-1', 'post_type' => 'glossary_term','orderby' => 'title', 'order' => 'ASC')); | |
//print_r($glossary_posts); | |
//create list of all distinct first letter | |
$prev_letter = ''; | |
$distinct_letters = array(); | |
$i = 0; | |
foreach ($glossary_posts as $term){ | |
$first_letter = substr($term->post_title, 0,1); | |
if($first_letter != $prev_letter){ | |
$distinct_letters[$i] = $first_letter; | |
$prev_letter = $first_letter; | |
$i++; | |
} | |
} | |
//print_r($distinct_letters); | |
//display navigation | |
echo '<ul id="glossary-nav">'; | |
foreach(range('A','Z') as $i){ | |
echo '<li>'; | |
if(in_array($i, $distinct_letters)){ | |
echo '<a href="#' . $i . '">' . $i . '</a>'; | |
} | |
else | |
echo $i; | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
$prev_letter =''; | |
$loop = new WP_Query(array('post_type' => array('glossary_term'), 'posts_per_page' => '-1', 'orderby' => 'title', 'order' => 'ASC')); | |
if($loop->have_posts()) : | |
while($loop->have_posts()): | |
$loop->the_post(); | |
echo '<div class="post">'; | |
$first_letter = substr(get_the_title(), 0, 1); | |
if($first_letter != $prev_letter) { | |
the_title('<a class="letter-index" id="' . $first_letter . '"></a><h2>','</h2>'); | |
$prev_letter = $first_letter; | |
} | |
else | |
the_title('<h2>','</h2>'); | |
the_content(); | |
echo "</div>"; | |
endwhile; | |
else : | |
echo 'No documents matched the search criteria.'; | |
endif; | |
genesis_posts_nav(); | |
} | |
genesis(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment