0

I found some code for creating multi-column WordPress loops and I found some code for entries grouped by letter of the alphabet. But I can't quite get the code to work together...the order is a bit messed up.

EXAMPLE

http://52words.org/w/

I would appreciate some assistance.

<style>
.col { width:24%; float:left; margin-right:5px; }
</style>

<?php
$num_cols = 4; // set the number of columns here
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // for pagination
$args = array(
  'orderby' => 'title',
  'order' => 'ASC', 
  'posts_per_page' => '199',
  'paged' => $paged,
  'post_type' => 'wiki'  
);
query_posts($args);
//end of query section
if (have_posts()) :
  for ( $i=1 ; $i <= $num_cols; $i++ ) :
    echo '<div id="col-'.$i.'" class="col">';
    $counter = $num_cols + 1 - $i;
    while (have_posts()) : the_post();
      if( $counter%$num_cols == 0 ) : ?>

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

<li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php _e( 'Permanent Link to', 'buddypress' ); ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

<?php }   ?>

      <?php endif;
      $counter++;
    endwhile;
    rewind_posts();
    echo '</ul></div>'; //closes the column div
  endfor; ?>
3

So, from what I can tell, you want four columns of relatively equal length on a page, with a title letter every time you move on to a new letter of the alphabet.

<?php
$num_cols = 4; // set the number of columns here
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // for pagination
$args = array(
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page' => '199',
  'paged' => $paged,
  'post_type' => 'wiki'
);
$wikis = new WP_Query($args);
//end of query section

if ($wikis->have_posts()) :
  // figure out where we need to break the columns
  // ceil() rounds up to a whole number
  $break_at = ceil( $wikis->post_count / $num_cols );
  // start with the first column
  $col_counter = 1;
  $post_counter = 1;
  // Set the title letter empty so that it's always output at the beginning of the cols
  $initial = '';
  ?>
  <div id="col-<?php echo $col_counter ?>" class="col">

  <?php while ($wikis->have_posts()) : $wikis->the_post();

    // Start a new column (but not the first one)
    if( $post_counter % $break_at == 0 && $post_counter > 1 ) :
      $col_counter++;
      ?>
      </ul></div>
      <div id="col-<?php echo $col_counter ?>" class="col"><ul>
    <?php endif;

    $title = get_the_title();
    $wiki_letter = strtoupper(substr($title,0,1));

    if( $initial != $wiki_letter) : ?>
      <?php if ( $post_counter > 1 ) : // close the previous ul ?>
        </ul>
      <?php endif; ?>
      <h3><?php echo $wiki_letter ?></h3>
      <ul>
      <?php $initial = $wiki_letter;
    endif; ?>

    <li>
      <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php _e( 'Permanent Link to', 'buddypress' ); ?> <?php the_title_attribute(); ?>"><?php echo $title ?></a>
    </li>
    <?php $post_counter++; ?>
  <?php endwhile; ?>
  </ul>
</div>

<?php wp_reset_postdata();
endif; ?>

This code does have a small bug where if a new letter of the alphabet starts at the same time as a new column, there's an empty <ul> tag at the beginning of the column.

2
  • Thanks for your help. The code now sorts entries properly. But the h3 and ul tags are now prefaced before every link/list item, instead of grouping bullet lists by their first letter.
    – Richard
    Nov 22 '13 at 3:43
  • 1
    I misspelled '$initial' as '$inital` inside the if ($initial != $wiki_letter) block. It should work if you correct the spelling mistake.
    – Jen
    Nov 22 '13 at 15:17

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.