簡體   English   中英

最后一次循環時的PHP

[英]PHP while last loop

我有一個類似的代碼:

<?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 -->

我正在尋找無論多少個數組元素都將始終起作用的東西。

坦克!

這里有很多答案,但都沒有提到$wp_query->post_count$wp_query->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; ?>
<?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 ,只需根據您的需要進行調整,即可輕松更改循環中的最后一次迭代

您可以使用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'?>">

簡單快捷。 ;)

您可能要做的最好的事情就是將最終調用置於while循環之外,並更改while循環邏輯,以使其盡早退出。 代替:

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

做類似的事情

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

編輯 :我一直假設您不知道帖子的數量,直到您遍歷它們為止,並且返回直到帖子用盡的界面是唯一可用的界面。 如果您有一個計數,並且可以按數字進行訪問,那么其他建議也可以正常工作。 實際上,如果您可以指望數量很少的帖子,那么最好將它們讀入數組並使用for循環而不是while循環來完成。

<?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; ?>

如果您可以控制$ loop的數據結構,則可以將int轉換為迭代器,並在其頂部使用CachingIterator。 如果您不進行控制,則可能需要將Iterator作為包裝器進行構建:

<?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
    }
 }
 ?>

while ( $loop->have_posts() ) : $loop->the_post();之后,在循環中使用此代碼while ( $loop->have_posts() ) : $loop->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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM