Displaying terms by first letter

I run a book review blog and I have multiple pages that lists the reviews by different things (rating, book title, and author)

I have custom taxonomy for authors and I basically have my code list all the terms that have posts in them, then list the posts. They’re all in alpha order by the first letter. What I want to do is have it grab the first letter and section everything off that way. Right now it looks like:

Read More

Bray, Libba

  • Beauty Queens

Elkeles, Simone

  • Chain Reaction

and I want it to be like

B

Bray, Libba

  • Beauty Queens

E

Elkeles, Simone

  • Chain Reaction

So basically just displaying the terms as I have it, but having a section for each letter THEN display the terms starting with that letter, then listing the posts.

<?php
$taxonomy = 'authors';//  e.g. post_tag, category
$param_type = 'authors'; //  e.g. tag__in, category__in
$term_args=array(
  'orderby' => 'name',
  'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => $term->slug,
      'post_type' => 'reviews',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1,
      'meta_key' => 'booktitle', 
      'orderby' => 'meta_value',
      'order' => 'ASC'
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {

      echo '<h2>'.$term->name.'</h2>';
      while ($my_query->have_posts()) : $my_query->the_post(); 
  ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo c2c_get_custom('booktitle'); ?></a> by <?php echo c2c_get_custom('author'); ?><br />
       <?php
      endwhile;
    }
  }
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

Related posts

Leave a Reply

1 comment

  1. Try this code.

    <?php
    $taxonomy = 'authors';//  e.g. post_tag, category
    $param_type = 'authors'; //  e.g. tag__in, category__in
    $term_args=array(
                'orderby' => 'name',
                'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
        $first_letter = null;
        foreach( $terms as $term ) {
    
            $flag = 0;
    
            if( $first_letter != substr( $term->name, 0, 1 ) ) {
              $first_letter = substr( $term->name, 0, 1 );
              $flag = 1;
            }
    
            if( $flag ) {
              echo '<strong>'.$first_letter.'</strong>'; // Output the first letter
            }
    
            $args=array(
              "$param_type" => $term->slug,
              'post_type' => 'reviews',
              'post_status' => 'publish',
              'posts_per_page' => -1,
              'caller_get_posts'=> 1,
              'meta_key' => 'booktitle', 
              'orderby' => 'meta_value',
              'order' => 'ASC'
              );
    
            $my_query = null;
            $my_query = new WP_Query($args);
            if( $my_query->have_posts() ) {
                echo '<h2>'.$term->name.'</h2>';
                while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo c2c_get_custom('booktitle'); ?></a> by <?php echo c2c_get_custom('author'); ?><br /><?php
                endwhile;
            }
        }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>