【问题标题】:PHP while last loopPHP while 最后一个循环
【发布时间】:2011-06-17 06:49:49
【问题描述】:

我有这样的代码:

<?php $loop = some array with posts;
      while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="class">
                <p>(data)</p>
             </div>   
     <?php endwhile; ?>

现在我想为最后一个循环更改 div 类(从“class”到“class2”)。该怎么做?

例子:

当“一些带有帖子的数组”现在有 4 条记录时,我得到:

<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>

我想得到:

<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class"><p>data</p></div>
<div class="class2"><p>data</p></div> <!-- this one is different -->

无论有多少数组元素,我都在寻找始终有效的东西。

坦克!

【问题讨论】:

标签: php while-loop wordpress


【解决方案1】:

这里有很多答案,但没有一个提到 $wp_query-&gt;post_count$wp_query-&gt;current_post 在循环中都很有用:

<?php 
$loop = array(); //some array with posts;
while ( $loop->have_posts() ) : 
    $loop->the_post();
    $class = 'class';
    if($loop->post_count == $loop->current_post+1){ // if this is the last item
        $class = 'class2';
    }?>
        <div class="<?php print $class ;?>">
            <p>(data)</p>
         </div>   
 <?php endwhile; ?>

【讨论】:

  • 我猜这是正确的答案,例如current_post 以零而不是一开头,所以 post_count == current_post 永远不会是真的。猜这个更正确:$loop-&gt;post_count == ($loop-&gt;current_post + 1)
  • 感谢@honk31 未经测试的伪代码的问题是你实际上并没有测试它,当然 current_post 是一个索引值!
【解决方案2】:

您可以使用 WP_query 对象。您有两个有用的实例(post_count 和 current_post)。

// Example
<?php $query = new WP_Query(array('cat' => 112));
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

然后

<div class="post<? if(($query->current_post + 1) == $query->post_count) echo ' last'?>">

简单快捷。 ;)

【讨论】:

    【解决方案3】:

    在循环中使用这段代码,紧跟在while ( $loop-&gt;have_posts() ) : $loop-&gt;the_post();之后

    <?php
    $postCount = 0;
    
    // Start of The Loop
    
    if (++$postCount == 1) {
         // FIRST POST
    } 
    
    if ($postCount == sizeof($posts)) {
         // LAST POST IN LOOP
    }
    
    // End of The Loop
    ?>
    

    来源:
    http://wordpress.org/support/topic/last-post-in-the-loop?replies=4#post-954762

    【讨论】:

      【解决方案4】:

      如果您可以控制 $loop 的数据结构,您可以将 int 转换为迭代器并在其上使用 CachingIterator。如果你不控制更复杂的可能是构建一个迭代器作为包装器:

      <?php
      class PostIterator implements Iterator {
          private $have_post;
      
          function __construct($loop) {
              $this->loop = $loop;
          }
      
          function rewind() {
              $this->have_post = $this->loop->have_posts(); // looks like we have to call "next" to get the first element, too
          }
      
          function next() {
              $this->have_post = $loop->have_posts();
          }
      
          function valid() {
              return $this->have_post;
          }
      
          function key() {
              return 0; // not used in this case, better implementation might have aocunter or such
          }
      
          function current() {
               return $this->loop->the_post();
          }
      }
      
      $ci = new CachingIterator(new PostIterator($loop)
      foreach ($ci as $data) {
          if (!$ci->hasNext()) {
               // last element
          }
       }
       ?>
      

      【讨论】:

        【解决方案5】:

        您能做的最好的可能是将最终调用放在 while 循环之外,并更改您的 while 循环逻辑,使其提前退出。而不是:

        while ($post = getPost()) {
            printPost($post);
        }
        

        做类似的事情

        $last = getPost();
        $post = getPost();
        while ($post != null) {
            printPost($last);
            $last = $post;
            $post = getPost();
        }
        printSpecial($post);
        

        编辑:我一直假设您在迭代它们之前不知道帖子的数量,并且返回帖子直到用尽的界面是唯一的可用的。如果您有计数并且可以通过数字访问它们,那么其他建议将正常工作。事实上,如果您可以指望帖子数量很少,那么您最好将它们读入一个数组并使用for 循环而不是while 循环来完成。

        【讨论】:

          【解决方案6】:
          <?php $loop = some array with posts;
                while ( $loop->have_posts() ) : $loop->the_post();
                   $class = $loop->have_posts() ? 'class' : 'class2'; ?>
                      <div class="<?php echo $class ?>">
                          <p>(data)</p>
                       </div>   
               <?php endwhile; ?>
          

          【讨论】:

            【解决方案7】:
            <?php
            $num_rows = mysql_num_rows($result1);
            $i = 0;
            while ($row = mysql_fetch_array($result1, MYSQL_ASSOC)) {
                $i++;
                /*
                your code
                 */
                if ( $i == ( $num_rows - 1 ) )
                    //you're on last line...    
            }
            ?>
            

            取自 http://www.phpbuilder.com/board/showthread.php?t=10359504 这里,只需根据您的需要进行调整,您就可以轻松更改循环中的最后一次迭代

            【讨论】:

            • 请注意,这会在每次页面加载时添加额外的数据库命中,您可能需要考虑在 while 循环之前运行 mysql_fetch_array,然后仅对结果数组进行操作以保存数据库命中。
            • 嗨 eykanal,这个问题与 mysql 数据库无关,但这是我为 O/P 建议的循环位
            • +1 - 用户只需要一个基本的计数器(mysql部分无关,只是示例的一部分)。
            • 如果提问者使用wp_query()产生$loop,他们可以使用$loop-&gt;post_count得到帖子总数。请参阅codex
            猜你喜欢
            • 2011-10-12
            • 2017-02-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多