【问题标题】:PHP: Break loop and then continue?PHP:中断循环然后继续?
【发布时间】:2012-10-18 17:38:15
【问题描述】:

我的 WordPress 帖子循环有问题。我想分解帖子循环,在帖子中添加一些 HTML div 标签。例如,我试图在第一次发布后在帖子周围添加一个 div .wrap。因为是贴贴,所以应该只有一次。

问题在于 .wrap div 由于循环而被重复。如何打破循环,只添加一次.wrap div,然后继续循环?

这是我的代码:

while ($wp_query->have_posts()) :

    $wp_query->the_post(); 
    if($wp_query->current_post <= 1){ ?>
        <div class="x">
        .. post 1
        </div>
    <?php }
    if($wp_query->current_post > 1){ ?>
        <div class="wrap"> <!-- should be only once -->
            <div class="y">
            .. post 2
            </div>

            <div class="y">
            .. post 3
            </div>
        </div><!-- should be only once -->
    <?php } ?>

endwhile;

【问题讨论】:

  • 你不必打破循环
  • 是的,换行输出代码不循环。

标签: php html wordpress


【解决方案1】:

试试这个

<?php
while ($wp_query->have_posts()) :
    $wp_query->the_post(); 
    if($wp_query->current_post <= 1){ ?>
        <div class="x">
        .. post 1
        </div>
        <div class="wrap"> <!-- should be only once -->
    <?php 
    $is_wrapped = 1;
    }
    if($wp_query->current_post > 1){ ?>
        <div class="y">
          .. post 2, then post 3, ...
        </div>
    <?php } 
endwhile;?>
<?php if (!empty($is_wrapped)){ ?>
        </div><!-- should be only once -->
<?php } ?>

【讨论】:

    【解决方案2】:

    您可以只使用迭代器索引,如下所示:

    $i = 0;
    while (...) {
        if ($i == 1) {
           // actions you need to do only once
        }
    
        ...
        $i++
    }
    

    【讨论】:

      【解决方案3】:
      <div class="x">
      
      while ($wp_query->have_posts()) :
          $wp_query->the_post();
          <--print post-->
          if($wp_query->current_post <= 1){
              </div><div class="y">
          }
      endwhile;
      
      </div>
      

      如果没有帖子,您将获得一个空的&lt;div class="x" /&gt;,如果只有一个帖子,您将获得一个空的&lt;div class="y" /&gt;。但在你的情况下这可能是可行的......


      编辑:示例

      <?php
      $wp_query = new WPDummy; 
      
      echo '<div class="x">';    
      while ($wp_query->have_posts()) :
          $wp_query->the_post();
          $wp_query->the_content();
          if($wp_query->current_post <= 1){
              echo '</div>', "\n",'<div class="y">';
          }
      endwhile;
      echo '</div>';
      
      // boilerplate
      class WPDummy {
          public $current = null;
          public $current_post = 0;
          public function __construct() {
              $this->posts = array( array('content'=>'A'),array('content'=>'B'),array('content'=>'C'),array('content'=>'D') );
          }
      
          public function have_posts() {
              return false!=current($this->posts);
          }
      
          public function the_post() {
              $this->current_post+=1;
              $this->current = current($this->posts);
              next($this->posts);
          }
      
          public function the_content() {
              echo '<span class="content">', $this->current['content'], '</span>';
          }
      }
      

      打印

      <div class="x"><span class="content">A</span></div>
      <div class="y"><span class="content">B</span><span class="content">C</span><span class="content">D</span></div>
      

      【讨论】:

      • 我的问题是只在循环内添加一个额外的 div 包装一次。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多