Created
July 24, 2019 20:28
-
-
Save bmrafiq/56b456ebb5ee6c229ab403a5d57a0fec to your computer and use it in GitHub Desktop.
This file contains hidden or 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( | |
array( | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC', | |
) | |
); | |
$alphas = range('A', 'Z'); | |
$letter_keyed_posts = array(); | |
if ( $posts ) { | |
foreach ( $posts as $post ) { | |
$first_letter = strtoupper( substr( $post->post_title, 0, 1 ) ); | |
if ( ! array_key_exists( $first_letter, $letter_keyed_posts ) ) { | |
$letter_keyed_posts[ $first_letter ] = array(); | |
} | |
$letter_keyed_posts[ $first_letter ][] = $post; | |
} | |
} | |
foreach ($letter_keyed_posts as $key => $value) { | |
?> | |
<table class="table"> | |
<caption><h1 class="text-left">List of --> <?php echo $key; ?></h1></caption> | |
<thead> | |
<tr> | |
<th scope="col">#</th> | |
<th scope="col">Title</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach ($value as $val) { | |
?> | |
<tr> | |
<th scope="row"><?php echo $val->ID; ?></th> | |
<td><a href="<?php echo get_permalink( $val->ID ); ?>"><?php echo $val->post_title; ?></a></td> | |
</tr> | |
<?php | |
} ?> | |
</tbody> | |
</table> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment