问题描述

如果我有一个从类别查询运行的循环,如:

<?php $the_query = new WP_Query('cat=1&showposts=50&orderby=title&order=asc');?>
<ul>
<?php while ($the_query->have_posts()) : $the_query->the_post();?>
<li>.. </li><?php wp_reset_query(); ?>
<?php endwhile; ?>
</ul>

我将如何创建一个 if 子句,以一定的间隔打破列表,并启动一个新的。所以例如在第 10 个帖子,返回一个</ul> 并在 11 开始一个新的<ul>

这是不正确的,但为了说明我的目标:

<?php $count =0;
    while($count <=50){
        if ($count == 9){
            echo "<li><a href='<?php%20the_permalink();%20?>'>
                      <?php the_title(); ?></a></li></ul>";
            }
        elseif ($count == 10){
        echo "<ul><li><a href='<?php%20the_permalink();%20?>'>
                          <?php the_title(); ?></a></li>";
        }
        else {
        echo "<li><a href='<?php%20the_permalink();%20?>'><?php the_title(); ?></a></li>";
        }

将此逻辑包括在循环中的正确方法是什么?

最佳解决方案

为您的查询创建列,并轻松显示

在主题中可能更有用的东西适合于模板标签和循环。我的第一个答案并没有那么重视。此外,我认为这是一个太复杂的一个快速采用。

我想到的一个更简单的方法就是扩展”the loop” 的列,到目前为止这个解决方案:

一个 WP_Query_Columns 对象”extends” 任何标准 WP 查询与列可以很容易地迭代。第一个参数是查询变量,第二个参数是每列显示的项目数:

<?php $the_query = new WP_Query('cat=1&showposts=50&orderby=title&order=asc');?>
<?php foreach(new WP_Query_Columns($the_query, 10) as $column_count) : ?>
    <ul>
        <?php while ($column_count--) : $the_query->the_post(); ?>
        <li><a href="<?php%20the_permalink();%20?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
    </ul>
<?php endforeach; ?>

要使用它,只需将 WP_Query_Columns class from this gist 添加到您的主题 function.php 。

高级用法

如果您需要正在显示的列号 (例如,对于某些偶数/奇怪的 CSS 类,您可以从 foreach 获取列号:

<?php foreach(new WP_Query_Columns($the_query, 10) as $column => $column_count) : ?>

并且列的总数也可用:

<?php
    $the_columns = new WP_Query_Columns($the_query, 10);
    foreach($the_columns as $column => $column_count) :
?>
    <h2>Column <?php echo $column; ?>/<?php echo sizeof($the_columns); ?></h2>
    <ul>...

二十个例子

我可以快速地打入二十十个主题进行测试,并以这种方式在任何循环之上添加标题。它被插入到 loop.php 中,开始是主题的代码:

<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
    <div id="post-0" class="post error404 not-found">
        <h1 class="entry-title"><?php _e( 'Not Found', 'twentyten' ); ?></h1>
        <div class="entry-content">
            <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyten' ); ?></p>
            <?php get_search_form(); ?>
        </div><!-- .entry-content -->
    </div><!-- #post-0 -->
<?php endif; ?>

<!-- WP_Query_Columns -->
<?php
    ### Needs WP_Query_Columns --- see http://wordpress.stackexchange.com/q/9308/178
    $query_copy = clone $wp_query; // save to restore later
    foreach( new WP_Query_Columns($wp_query, 3) as $columns_index => $column_count ) : ?>
    <ul>
        <?php
        while ( $column_count-- ) : the_post(); ?>
            <li><h2 class="entry-title"><a href="<?php%20the_permalink();%20?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2></li>
        <?php endwhile; ?>
    </ul>
<?php endforeach; ?>
<?php $wp_query = $query_copy;?>

<?php
    /* Start the Loop.
    ...

对于更长的答案:

(这基本上是我以上的东西,但是更好地解释了如何用简单的数学操作来解决问题,我的新解决方案是迭代一些 pre-calculated)

这取决于你实际需要多少解决问题。

例如,如果每列的项数等于 1,这很简单:

<?php $the_query = new WP_Query('cat=1&showposts=50&orderby=title&order=asc');?>
<?php while ($the_query->have_posts()) : $the_query->the_post();?>
<ul>
    <li>.. </li>
<ul>
<?php endwhile;  wp_reset_query(); ?>
</ul>

即使使用这个简单的代码,也可以看出有多种决定要做:

  • 一列中有多少项?

  • 总共有多少物品?

  • 是否有一个新的列开始?

  • 还有一列要结束吗?

最后一个问题是非常感兴趣的 HTML 输出,因为您可能不仅要包含项目,还包含 html 元素的列。

幸运的是,使用代码,我们可以设置所有这些变量,并创建始终根据需要计算的代码。

有时候甚至我们从一开始就不能回答每一个问题。对于例子,总项目的计数:是否有一些,一些,多个,一个完整的计数匹配总共的整数列?

即使 Jan Fabry 的答案可能在某些情况下工作 (如上例所述,对于 one-item-per-column 方案),您可能对 WP_Query 返回的任意数量的项目感兴趣。

首先为数学:

//
// arithmetical example:
//
# configuration:
$colSize = 20;  // number of items in a column
$itemsTotal = 50; // number of items (total)

# calculation:
$count = 0; // a zero-based counter variable
$isStartOfNewColum = 0 === ($count % $colSize); // modulo operation
$isEndOfColumn = ($count && $isStartOfNewColum) || $count === $itemsTotal; // encapsulation

该代码不运行,所以让我们把它放在一个简单的文本示例中

//
// simple-text example:
//
$column = 0; // init a column counter
for($count=0; $count<= $itemsTotal; $count++) {
    $isStartOfNewColum = 0 === ($count % $colSize); // modulo
    $isEndOfColumn = ($count && $isStartOfNewColum);
    $isStartOfNewColum && $column++; // update column counter

    if ($isEndOfColumn) {
        printf("/End of Column: %dn", $column-1);
    }

    if ($isStartOfNewColum) {
        printf("<start of Column: %dn", $column);
    }

    printf(" * item %dn", $count);
}
if ($count && !$isEndOfColumn && --$count === $itemsTotal) {
    printf("/End of Column: %dn", $column);
}

printf("Done. Total Number of Columns: %d.n", $column);

这实际上运行并已经做了一些输出:

<start of Column: 1
 * item 0
 * item 1
 * item 2
 * item 3
...
 * item 17
 * item 18
 * item 19
/End of Column: 1
<start of Column: 2
 * item 20
 * item 21
 * item 22
...
 * item 37
 * item 38
 * item 39
/End of Column: 2
<start of Column: 3
 * item 40
 * item 41
 * item 42
...
 * item 48
 * item 49
 * item 50
/End of Column: 3
Done. Total Number of Columns: 3.

这个模拟已经很好了,它可以如何在 wordpress 模板中看起来像:

//
// wordpress example:
//
$count = 0; // init item counter
$column = 0; // init column counter
$colSize = 10; // column size of ten this time
$the_query = new WP_Query('cat=1&showposts=50&orderby=title&order=asc');
$itemsTotal = $the_query->post_count;
?>
<?php while ($the_query->have_posts()) : $the_query->the_post();?>
<?php
    # columns display variables
    $isStartOfNewColum = 0 === ($count % $colSize); // modulo
    $isEndOfColumn = ($count && $isStartOfNewColum);
    $isStartOfNewColum && $column++; // update column counter

    if ($isEndOfColumn) {
        print('</ul>');
    }

    if ($isStartOfNewColum) {
        printf('<ul class="col-%d">', $column);
    }
?>
    <li> ... make your day ...
    </li>
<?php endwhile; ?>
<?php
if ($count && !$isEndOfColumn && --$count === $itemsTotal) {
    print('</ul>');
}
// You don't have to do this in every loop, just once at the end should be enough
wp_reset_query();
?>

(我没有在 WP 环境中执行最后一个例子,但应该至少在语法上是正确的。)

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。