• Hey Gang,

    I have a task that requires an alpha list of posts, with letter headings. I managed to find some resources to help with this, and it works great. I am looking to tweak it though and need some help….

    I would like to have the ’empty’ letters be listed/as a subhead… but with not listings below.

    Sort of like this:

    ABCDEFGH…

    A
    -A post 1
    -A post 2

    B
    -B post 1
    -B post 2

    C
    (No listings)

    D
    -D post 1
    -D post 2

    <div class="alpha">
    <?php for($i=ord('A');$i<=ord('Z');$i++) {
      echo '<a class="alphalist" href="#'.chr($i).'">'.chr($i).'</a>';
    } ?>
    </div>
    
    <?php
    $args=array(
      'cat' => '-52,-101',
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        $this_char = strtoupper(substr($post->post_title,0,1));
        if ($this_char != $last_char) {
          $last_char = $this_char;
          echo '<a name="'.$last_char.'"></a><h2>'.$last_char.'</h2>';
        } ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br/>
        					<?php echo get_post_meta($post->ID, 'description', true); ?>
    
        </p>
        <?php
      endwhile;
    }
    wp_reset_query();
    ?>

    Any and all help is much appreciated.

    Thanks.
    Adam

Viewing 5 replies - 1 through 5 (of 5 total)
  • I think you can get what you want by changing this:

    if ($this_char != $last_char) {
          $last_char = $this_char;

    to this:

    if ($this_char != $last_char) {
           $last_ord = ord($last_char);
           while ( $last_char >= 'A' && (ord($this_char) - $last_ord) > 1) {
              ++$last_ord;
              $last_char = chr($last_ord);
              echo '<a name="'.$last_char.'"></a><h2>'.$last_char.'</h2><p>(No Listings)</p>';
           }
           $last_char = $this_char;

    Note that any Posts with non-alpha first characters are not getting a link in the top menu and that non-alpha characters with no posts will be skipped.

    Thread Starter coyacreative

    (@coyacreative)

    Worked perfectly.
    Thanks so much!

    Thread Starter coyacreative

    (@coyacreative)

    Does it make any sense that only the letters XYZ have been disabled?

    <div class="alpha">
    <?php for($i=ord('A');$i<=ord('Z');$i++) {
      echo '<a class="alphalist" href="#'.chr($i).'">'.chr($i).'</a>';
    } ?>
    </div>
    
    <?php
    $args=array(
      'cat' => '-52,-101',
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        $this_char = strtoupper(substr($post->post_title,0,1));
    if ($this_char != $last_char) {
           $last_ord = ord($last_char);
           while ( $last_char >= 'A' && (ord($this_char) - $last_ord) > 1) {
              ++$last_ord;
              $last_char = chr($last_ord);
              echo '<a name="'.$last_char.'"></a><h2>'.$last_char.'</h2><p>(No Listings)</p>';
           }
           $last_char = $this_char;
          echo '<a name="'.$last_char.'"></a><h2>'.$last_char.'</h2>';
        } ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br/>
        					<?php echo get_post_meta($post->ID, 'description', true); ?>
    
        </p>
        <?php
      endwhile;
    }
    wp_reset_query();
    ?>

    They appear in the nav. but do not display ‘No Listing’.

    Your Loop exits when there are no more posts. You will need to code an additional loop after the while loop to pick up any letters after the last one found in the query.

    I have not tested it, but I believe that you can use the embedded while loop from the code posted above:

    <?php
      endwhile;
    }
    while ( $last_char >= 'A' && (ord($this_char) - $last_ord) > 1) {
      ++$last_ord;
      $last_char = chr($last_ord);
      echo '<a name="'.$last_char.'"></a><h2>'.$last_char.'</h2><p>(No Listings)</p>';
    }
    wp_reset_query();
    Thread Starter coyacreative

    (@coyacreative)

    Thanks, vtxyzzy. I will give it a shot.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘List posts by Alpha w/ letter headings’ is closed to new replies.