Hi, I know I’m posting to an old thread, but this was pretty helpful when figuring out how to tack a similar issue. I needed the taxonomy lists to show up on separate pages because they were going to be so long. Each letter containing a minimum of 50 terms.
I think, however, that creating a page template for 26 letters is a little tedious.
So I decided to go the form route. Here is what I did:
Make a form that posts to the current page:
Make a form that posts to current page.
<p>Browse Categories Alphabetically</p>
<form action='#' method='post' name='form_filter' class="region">
<select name="alphabet">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
<option value="K">K</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="N">N</option>
<option value="O">O</option>
<option value="P">P</option>
<option value="Q">Q</option>
<option value="R">R</option>
<option value="S">S</option>
<option value="T">T</option>
<option value="U">U</option>
<option value="V">V</option>
<option value="W">W</option>
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
</select>
<input type="submit" value="Filter">
</form>
Write code to generate terms dynamically based on form selection:
<?php
$alphabet = $_POST['alphabet'];
if (isset($alphabet)) {
$terms = get_terms("directory_cat");
$count = count($terms);
$letter = $alphabet;
if ( $count > 0 ){
$ul = "<ul>";
foreach ( $terms as $term ) {
if ( preg_match("/^$letter/i",$term->name) ) {
echo $ul;
$ul = ''; // only display once
echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
}
}
if ( $ul == '') {
echo "</ul>";
}
}
} else {
$terms = get_terms("directory_cat");
$count = count($terms);
$letter = 'A';
if ( $count > 0 ){
$ul = "<ul>";
foreach ( $terms as $term ) {
if ( preg_match("/^$letter/i",$term->name) ) {
echo $ul;
$ul = ''; // only display once
echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
}
}
if ( $ul == '') {
echo "</ul>";
}
}
}
?>
One template. One piece of code. DONE.
Thanks for the help, I hope this post makes someone else’s life mildly less tedious :p