Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmrafiq/56b456ebb5ee6c229ab403a5d57a0fec to your computer and use it in GitHub Desktop.
Save bmrafiq/56b456ebb5ee6c229ab403a5d57a0fec to your computer and use it in GitHub Desktop.
<?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