Last active
February 22, 2017 19:24
-
-
Save pelmered/ae161bfa87507ee421ba02d48697c3b4 to your computer and use it in GitHub Desktop.
Letter post list
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 | |
$posts = get_posts([ | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'post_type' => 'post' | |
]); | |
$post_index = array_fill_keys( array_merge( [ '#' ], range( 'a', 'z' ), ['å', 'ä', 'ö', ] ), [] ); | |
foreach( $posts as $post ) { | |
$first_letter = strtolower( substr( $post->post_title, 0, 1 ) ); | |
if( is_numeric( $first_letter ) ) { | |
$post_index[ '#' ][] = $post; | |
} else { | |
$post_index[ $first_letter ][] = $post; | |
} | |
} | |
$curent_letter = ''; | |
echo '<ol>'; | |
foreach( $post_index as $letter => $letter_list ) { | |
echo '<li>' . $letter . '</li>'; | |
echo '<ol>'; | |
foreach( $letter_list as $post ) { | |
echo '<li>' . $post->post_title . '</li>'; | |
} | |
echo '</ol>'; | |
} | |
echo '</ol>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment