所以,据我所知,你希望在一页上有四列长度相对相等的列,每次移动到字母表中的新字母时,都有一个标题字母。
<?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>
在列的开头标记。