1

0 down vote favorite

I have a list of posts on my website, what I'm trying to do is to wrap them alphabetically from A-Z by title to get a glossary like this :

A. Apple

B. Banana

C. Carotts

D.

E.

F.

G. Grenada

and so on untill letter z.

I want the letter to be displayed even if there's no post.

and i want to wrap results inside this structure :

<div class="group_letter">
<div class="letter">A</div>
<div class="post">Apple</div>
</div>

<div class="group_letter">
<div class="letter">B</div>
<div class="post">Banana</div>
</div>

here is what I've got so far :

<?php 
$letter=' '; 
query_posts( array ( 'post_type' => 'auteurs', 'orderby' => 'title', 'order' => 'ASC' ) );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php 
$title=get_the_title(); 
$initial=strtoupper(substr($title,0,1));
if($initial!=$letter) {
echo "<div>$initial</div>";
$letter=$initial;
}

echo "<div class='post'>" . $title. "</div>";
?>

<?php endwhile; endif; wp_reset_query(); ?>

here is the result :

<div class='letter'>A</div>
<div class='post'>Apple</div>    

<div class='letter'>B</div>
<div class='post'>Banana</div>    

<div class='letter'>C</div>
<div class='post'>carotts</div>   

<div class='letter'>G</div>
<div class='post'>Grenanda</div>

I have 2 problems :

  • Empty letters are not displayed.
  • I can't find a way to wrap my groups inside group_letter div.

can anybody help me with this ?

thanks a lot for your help

2
  • start by not using query_posts Jan 20 '18 at 18:04
  • When there are numerous posts under each letter, do you want to output the letter each time, or just output it once at the top of the list - the latter being more typical glossary style. It's not extremely difficult to achieve, but does require extra coding. Similarly for initial articles - the, a, an: Easiest but atypical just to alphabetize them like any other words. (Your code snippet suggests you're more likely dealing with names, which might raise the question of last names vs first names.)
    – CK MacLeod
    Jan 21 '18 at 17:24
3

I would do it the other way around- loop over the alphabet and check all posts for a match with each letter.

$all_posts = new WP_Query(
    array(
        'orderby' => 'title',
        'order' => 'ASC',
        'posts_per_page' => -1
    )
);

if( $all_posts->have_posts() ){
    foreach( range( 'A', 'Z' ) as $letter ) {
        echo '<div class="group_letter"><div class="letter">' . $letter . '</div>';
        while( $all_posts->have_posts() ){
            $all_posts->the_post();
            $title = get_the_title(); 
            $initial = strtoupper( substr( $title, 0, 1 ) );
            if( $initial == $letter ){
                echo '<div class="post">' . $title . '</div>';
            }
        }
        $all_posts->rewind_posts();
        echo '</div>';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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