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 would like to create an A to Z index of posts in a certain category. This must include a linkable directory of letters ABCDEF .... etc and a list of articles ordered by title grouped by letter.

for example

A 
A first article 
A second article

B 
B first article
B Second aericle 

C

when some one clicks on the index (ABCDE .....) link him to the letter group....

Any ideas?

share|improve this question
1  
Downvoted, due to the scope of this question. It is at least two (if not more) questions in one, in a "do-work-for-me" format: query posts by category, create letter index, sort posts by first letter. –  Chip Bennett Apr 9 '12 at 14:55

1 Answer 1

It's easy if all posts are shown on the same page, then you can do:

$AZposts = get_posts(array(
    'numberposts'     => -1,
    'post_type'       => 'post',
    'orderby'         => 'title',
    'order'           => 'ASC',
    'category'    => $cat
));

$current = "";
$nav = "";
$postlist = "";
foreach($AZposts as $AZpost) {
    $firstletter = strtoupper(substr($AZpost->post_title,0,1));
    if($firstletter != $current) {
        $postlist .= "<b><a name='$firstletter'> $firstletter </a></b><br>\n";
        $nav .= "<a href='#$firstletter'> $firstletter </a> ";
        $current = $firstletter;
    }
    $postlist .= $AZpost->post_title . "<br>\n";
}

print $nav . "<br>" . $postlist;
share|improve this answer
    
get_permalink( $AZpost->ID ) would be probably useful. Besides that: Nice, canonical answer. +1 :) –  toscho Apr 9 '12 at 21:17

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.