开发者

PHP while last loop

I have a code like:

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

Now I开发者_如何转开发 want to change the div class (from "class" to "class2") for the very last loop. How to do that?

Example:

When "some array with posts" have now 4 records then I'm getting:

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

And I want to get:

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

I'm looking for something that will work always no matter how many array elements there will be.

Tanks!


Lots of answers here but none refers to $wp_query->post_count and $wp_query->current_post both useful in the loop:

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

Taken from http://www.phpbuilder.com/board/showthread.php?t=10359504 here, just adapt this to your needs and you can easily change the last iteration in the loop


You can use a WP_query object. You have two useful instances (post_count and current_post).

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

Then

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

Easy and quick. ;)


The best you can do is probably to put the final call outside the while loop, and change your while loop logic so that it exits early. Instead of:

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

do something like

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

Edit: I've been assuming that you don't know the number of posts until you've iterated over them, and that the interface that returns posts until they are exhausted is the only one available. If you have a count and can access them by number, then the other suggestions will work fine. In fact, if you can count on the number of posts being small, then you might be best just to read them into an array and do it with a for loop rather than a while loop.


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


If you have control over the data structure of $loop you might convert int to an iterator and use a CachingIterator on top of it. If you don't control a bit more complicated might be to build an Iterator as wrapper:

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


Use this code in the loop, right after 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
?>

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜