WordPress – Archive with showing first characters as headline

I used a shortcode to include my own customized archive on a WordPress page.

Currently it looks like this:

Read More
- Here is title number 1
- Title 2

What I know want to achieve is something like this:

H:
- Here is title number 1

T:
- Title 2

Does somebody know if a function for that already exists somewhere? My code is currently pretty straight forward:

//[show_archive]
function create_archive( $atts ){

    $args = array(
        'post_type' => 'post',
        'orderby' => 'title',
        'order'   => 'ASC',
        'posts_per_page' => -1,
    );
    // The Query
    $the_query = new WP_Query( $args );
    // The Loop

    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'">' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        _e("Es sind keine beiträge gefunden worden.");
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}//function
add_shortcode( 'show_archive', 'create_archive' );

Thanks!

Related posts

1 comment

  1. while ($the_query->have_posts())
     {
     $the_query->the_post();
     $current_character = ucfirst(substr(get_the_title(),0,1));
     echo '<h2>'.$current_character.'</h2>';
     if ($last_character != $current)
      {
      echo '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</li>';
      $last_character = $current_character;
      }
     }
    

Comments are closed.