4列循环按字母顺序排序,条目按其第一个字母分组(和标记)

时间:2013-11-21 作者:Richard

我找到了一些创建多列WordPress循环的代码,还找到了一些按字母表字母分组的条目代码。但是我不能让代码一起工作。。。秩序有点混乱。

实例

http://52words.org/w/

我将非常感谢您的帮助。

<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; ?>

1 个回复
最合适的回答,由SO网友:Jen 整理而成

所以,据我所知,你希望在一页上有四列长度相对相等的列,每次移动到字母表中的新字母时,都有一个标题字母。

<?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; ?>
此代码确实有一个小错误,如果字母表中的一个新字母与一个新列同时开始,则会有一个空的<ul> 在列的开头标记。

结束

相关推荐

Get 1 more post in loop

例如,在简单循环中:$loop = new wp_query(array(\'posts_per_page\' => 3)); while($loop->have_posts()) : $loop->the_post(); if(get_post_meta($post->ID, \'skip_me\', true) == true){ // do something to ask for one more post, &#x