Sorry for the title, I didn’t know how to call it better.

I am working on a site on which I am building a listing of doctors being ordered alphabetically by last name/word of title, while all posts belonging to a letter should be grouped together below it, just like this:


  • Dr. John Arbuckle
  • Dr. Mack Ancie


  • Dr. Howie B.
  • Dr. Bones

The listing is fine, only I cannot get a div to wrap around each letter’s block enclosing the letter and its posts. The desired output for one block should be

<div class="letter_item">
    <li><a href="#link">Dr. John Arbuckle</a></li>
    <li><a href="#link">Dr. Mack Ancie</a></li>

So far, since I cannot properly close it, the lists are cascading (see live example link). The code I use is the following, and since it checks for the first letter and inserts the h2 only when a different letter is used, it cannot be used to wrap the code for the list.

<div class="section_index grid_12 section_aerzte" id="aerzte">
    $args = array(
        'orderby' => 'title',
        'order' => 'ASC',
        'post_type' => 'arzt',
        'caller_get_posts' => 1,
        'posts_per_page' => 20,

    function mam_posts_orderby ($orderby) {
    global $mam_global_orderby;
    if ($mam_global_orderby) $orderby = $mam_global_orderby;
    return $orderby;
    $mam_global_orderby = "
    UPPER(CONCAT(REVERSE(SUBSTRING_INDEX(REVERSE($wpdb->posts.post_title),' ',1)),$wpdb->posts.post_title))

    if (have_posts()) {
      $curr_letter = '';
      while (have_posts()) {

        $fullposttile = "$post->post_title";
        $words = explode(' ', $fullposttile);
        $lastWord = array_pop($words);

        $this_letter = strtoupper(substr($lastWord,0,1));
        if ($this_letter != $curr_letter) {
          echo "<div class='letter_item'><h2>$this_letter</h2><ul>";
          $curr_letter = $this_letter;

    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>

  <?php }
/** Here it goes wrong, obviously **/


If it helps, the live example of the test site is at http://www.physalis-web.com/aerztegemeinschaft/aerzte . Is there a (better) way to get a wrapping properly done? Any help greatly appreciated!

1 Answer 1


The problem is that each time you start a new letter, you don't close the list of the previous one.

Inside your check if ($this_letter != $curr_letter), before the echo, you have to add a </ul> in all cases except when $curr_letter is empty (the first letter).

if ($this_letter != $curr_letter) {
    if('' != $curr_letter){
        echo '</ul></div>';
    echo "<div class='letter_item'><h2>$this_letter</h2><ul>";
    $curr_letter = $this_letter;
  • Quite exactly, but how would I actually do that :D? And it would need to close the div as well, of course.
    – physalis
    Commented Nov 15, 2014 at 8:34
  • see edit above.
    – Milo
    Commented Nov 15, 2014 at 15:54
  • Hey there, I thought it must produce a problem with a left-over </ul></div>, but it’s actually fine and well-working. Thanks a lot, you nailed it!
    – physalis
    Commented Nov 17, 2014 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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