List pages by the first letter?
-
Hey guys,
does anyone know how to list pages based on their first letter? I need the code to alphabetically go through each letter, see if it has any pages associated with it, then echo that letter, with the list of pages to the side. An example is shown below:
https://yfrog.com/goscreenshot20101202at212p
I have managed to find a previous post that did the exact same thing but with taxonomy terms. the code is shown below:
$list = ”;
$args = array(
‘hide_empty’ => false // Set this to true to hide terms with no posts
);
$tags = get_terms(‘madeof’,$args);
$groups = array();if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}if( !empty( $groups ) ) {
$index_line = ‘<p class=”header-line”>A-Z Brands</p>’;
foreach( $groups as $letter => $tags ) {
$list .= “\n<p class=’letter-line’><span class=’first-letter’>” .
apply_filters( ‘the_title’, $letter ) . ‘</span> ‘;
foreach( $tags as $tag ) {
$name = apply_filters( ‘the_title’, $tag->name );
$list .= ‘<span class=”term-name”>’ . $name . “</span> “;
}$list .= ‘</p>’;
}$list .= ”;
}} else $list .= ‘<p>Sorry, but no tags were found</p>’;
//print htmlentities($index_line);
//print htmlentities($list);
print ($index_line);
print ($list);I have tried to amend it to do the same thing with pages but keep getting the dreaded white screen!
Any help would be much appreciated.
- The topic ‘List pages by the first letter?’ is closed to new replies.