【问题标题】:Nth iteration for wordpress while loopwordpress while 循环的第 N 次迭代
【发布时间】:2013-03-08 09:25:15
【问题描述】:

我知道这很容易,但无法用 wordpress while 循环解决问题。我正在使用 zurb 框架和使用以下 html 结构的产品列表。

<div class="row">
    <div class="four columns">
        item here
    </div>
    <div class="four columns">
        item here
    </div>
    <div class="four columns">
        item here
    </div>
</div>

所以在 while 循环中,我想在每三个四列 div 之后重复整个结构。我试过下面的代码

    <?php
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query->query('post_type=product' . '&paged=' . $paged .'');
    ?>

    <?php $counter = 0;

    if ( have_posts() ) : while ( have_posts() ) : the_post();
        if ($counter % 3 == 0):
        echo '<div class="row"><!--row starts here-->';
        endif; ?>

        <div class="four columns">
            <article>

                <header>
                    <hgroup>
                        <h2><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'foundation' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                        <h6>Written by <?php the_author_link(); ?> on <?php the_time(get_option('date_format')); ?></h6>
                    </hgroup>
                </header>

                <?php if ( has_post_thumbnail()) : ?>
                <a href="<?php the_permalink(); ?>" class="th" title="<?php the_title_attribute(); ?>" ><?php the_post_thumbnail(); ?></a>
                <?php endif; ?>

                <?php the_excerpt(); ?>

            </article>

        </div>

        <?php endwhile; ?>

    <?php else : ?>

        <h2><?php _e('No posts.', 'foundation' ); ?></h2>
        <p class="lead"><?php _e('Sorry about this, I couldn\'t seem to find what you were looking for.', 'foundation' ); ?></p>

    <?php endif;

    if ($counter % 3 == 0):
        echo '</div><!--row end here-->';
    endif; ?>

    <?php foundation_pagination(); ?>

</div>
<!-- End Main Content -->

但它在每个循环中都错误地重复。我知道这很明显可以重复,但我无法找到如何在仅 3 个四列之后重复的解决方案,并且在第一个循环行中也应该插入 div。

非常感谢

【问题讨论】:

    标签: wordpress while-loop zurb-foundation


    【解决方案1】:

    按照安迪所说的去做,在你的代码中添加代码$counter++;,在while循环内。

    例如,在您的代码中:

    </div>
    <?php $counter++; ?>
    <?php endwhile; ?>
    

    这显示了在您的示例中将其放在两条现有行之间的位置。

    【讨论】:

      【解决方案2】:

      您可能应该在循环中的计数器中添加一个,例如$counter++,然后关闭此问题。

      【讨论】:

      • 我已经尝试在打开和关闭 div 时使用 ($counter %3 == 1),但没有工作。你能帮我详细了解一下,以便我尝试解决这个问题
      • 啊!我已将 ($counter %3 == 0) 添加到打开 div 并将 ($counter %3 == 1) 添加到关闭 div 并且我认为它正在工作.. 让我通过添加四个或五个项目来检查它,如果它有效,它将关闭问题
      猜你喜欢
      • 1970-01-01
      • 2018-05-14
      • 2015-10-20
      • 2015-03-23
      • 2016-06-19
      • 2016-10-22
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多