Skip to content

Instantly share code, notes, and snippets.

@oskaryil
Created February 22, 2017 20:40
Show Gist options
  • Select an option

  • Save oskaryil/9a8a616d852d345d6df83fabd2030ce6 to your computer and use it in GitHub Desktop.

Select an option

Save oskaryil/9a8a616d852d345d6df83fabd2030ce6 to your computer and use it in GitHub Desktop.
<?php
$posts = get_posts([
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'abc'
]);
$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 type="A">';
foreach( $post_index as $letter => $letter_list ) {
echo '<li>';
echo '<ul>';
foreach( $letter_list as $post ) {
echo '<li>' . $post->post_title . '</li>';
}
echo '</ul>';
echo '</li>';
}
echo '</ol>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment