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.

On a particular archive page I've created, I'm listing a number of posts by post name and they're listed in alphabetical order. Is there away to have them separated even further?

As in all posts starting with "A" are grouped and have a heading of "A", then all posts starting with "B" are grouped and have a heading of "B", etc.

share|improve this question
    
FWIW I'm using a custom child theme on the Genesis framework and a custom post type and taxonomies. –  NW Tech Dec 18 '12 at 1:01
    
Could you post the code of what you've got currently? –  Stephen Harris Dec 18 '12 at 1:18
    
Here's all i have at the moment: pastebin.com/67SJ2Uvr –  NW Tech Dec 18 '12 at 3:30
    
You can customize the SQL directly using wp_query filters. –  Mridul Aggarwal Dec 18 '12 at 4:10

1 Answer 1

up vote 3 down vote accepted

You can do some tricky thing with PHP. Here is the algorithm you could use.

  1. Query posts to get posts as alphabetical order.
  2. for/while loop starts.
  3. $t = Get first character of the title string.
  4. $temp = '' empty string.
  5. if $t != $temp echo $t.
  6. set $t = $temp.
  7. endfor/endwhile.

Hope it make sense. The main idea is you check the first character of the title to a temp variable. When their is a mismatch it will print the character. So you will get a character printed out when you transit to '' to A, A to B and so on.. :)


Finally got some time. Here is a very basic code example with get_posts()

<?php

    $posts = get_posts($args);

        $temp = '';
    foreach($posts as $post):
        $title = get_the_title();
        $first_letter = strtoupper(substr($title, 0, 1)); 
        if($temp != $first_letter){
            echo $first_letter;
            $temp = $first_letter;  
        }
        ?>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php
    endforeach;

?>

This code show the letter if their is an change of first letter of title. Make sure you sort it alphabetically when calling get_posts(). Hope you get the idea.

share|improve this answer
    
You code works great. However, is it possible to group the posts together under their associated letter. so all posts starting with "A" would be grouped together the heading "A". As it is right now, each post has it's own heading based on the first letter of the post name. –  NW Tech Mar 21 '13 at 21:30

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.