Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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().
?>
share|improve this question
    
What I want to do is have it grab the first letter and section everything off that way Could you explain this a little better, I'm confused. –  BandonRandon Dec 18 '11 at 22:23
    
I want to have it show every term by the letter. I want to Have it show like -A- Term starting with A - Terms Post I the code I posted works to list all the terms that do have posts, and then displays the posts in that term. But I want to be able to show all the categories with A, then B, then C. But I need to figure out how to get it to pull the first letter, then to display all the terms that start with that letter that have posts, then show it's posts. –  Catiebug Dec 19 '11 at 2:04

1 Answer 1

up vote 2 down vote accepted

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().
?>
share|improve this answer
    
Yes! Thank you! Worked like a charm and does exactly what I want! I had been trying to figure it out for days now and nothing I did worked. So thank you so much. –  Catiebug Dec 19 '11 at 18:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.