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.

wrote this piece of code to make a alphabetical glossary and it works, but there is 1 problem. I don't know how I can end a set of posts with the same first letter.

This code looks at the first letter of the second word (this is for my project) and then it makes a heading with that first letter and places all the matching lines under that. But after that I want to close this set with an </div> (and open it first off course). But if I put the close div in my loop, the code places a </div> after each line.

So I need something that says: Close div after last line with the matching first letter. And then open a new div and do it again.

Hope you understand what I need. Thank you.

    <?php
     $obj = get_queried_object();
     $post_type = $obj->name;

    $args = array(
        'post_type' => $post_type,
        'orderby' => 'title',
        'order' => 'ASC',
        'posts_per_page' => 20,
    );

    query_posts($args);

    if (have_posts()) : $curr_letter = ''; while (have_posts()) : the_post();

    $title = get_the_title();
    $title_array = explode(' ', $title);
    $second_word = $title_array[1];

    $this_letter = substr($second_word, 0, 1);

    if ($this_letter != $curr_letter) {

    $curr_letter = $this_letter; ?> 

        <div id="sort-<?php echo strtolower($this_letter); ?>" class="alpha_title">
            <?php echo $this_letter; ?>
        </div><!--End alpha_title-->

    <?php } ?>

    <a href="<?php the_permalink(); ?>"><?php echo $second_word; ?></a>

    <?php endwhile; endif; ?> 

    <div class="clear"></div>

</div><!--End glossary-->
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

If you search the site, most of the "display nth post different" questions are very closely related to this one, and honestly this is more a PHP + logic question that a WordPress one.

if (have_posts()) { 
  $curr_letter = ''; 
  echo '<div class="alphawrapper">';
    while (have_posts()) {
      the_post();
      $title = get_the_title();
      $title_array = explode(' ', $title);
      $second_word = $title_array[1];
      $this_letter = substr($second_word, 0, 1);
      if ($this_letter != $curr_letter) {
        if (!empty($curr_letter)) {
          echo '</div><div class="alphawrapper">';
        }
        $curr_letter = $this_letter; ?> 
        <div id="sort-<?php echo strtolower($this_letter); ?>" class="alpha_title">
          <?php echo $this_letter; ?>
        </div><!--End alpha_title--><?php 
      } ?>
      <a href="<?php the_permalink(); ?>"><?php echo $second_word; ?></a><?php
    }
  echo '</div>';
} 

However, do not use query_posts. There are a thousand "tutorials" online recommending it, including parts of the Codex, but don't use it.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

Use a filter on pre_get_posts instead. In your theme's functions.php put:

function pregp_wpse_110875($qry) {
  if ($qry->is_main_query() && $qry->is_archive()) {
    $qry->set('posts_per_page',20);
    $qry->set('orderby','title');
    $qry->set('order','ASC');
  }
}
add_action('pre_get_posts','pregp_wpse_110875');
share|improve this answer
    
Okay thank you. So I should place it on Stackoverflow? And if I don't use query_posts, how can I modify the posts_per_page for different archive pages? –  Robbert Aug 21 '13 at 7:43
    
There is no reason to put this on Stack Overflow. It is perfectly on topic here. See the edit. –  s_ha_dum Aug 21 '13 at 13:49
add comment

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.