retrieving wordpress post with loop
I need to implement a slider which will show 4 post thumbnails from a particular category in each slide. For this I wrote this:
<ul class= "videoSlider">
<?php
$pStart = 0;
$flag = true;
开发者_如何学Python while ($flag) {
query_posts('cat=14&posts_per_page=4&offset='.$pStart);
$pStart = + 4;
?>
<li>
<?php
if (have_posts ()) {
while (have_posts ()) {
the_post();
?>
<div onclick="something()">
<?php echo the_post_thumbnail(array(215, 190)); ?>
</div>
<?php
}
} else {
$flag = false;
}
?>
</li>
<?php
wp_reset_query();
} ?>
The structure I need for the jquery slider is something like this:
<ui>
<li>
<div>
thumb 1
</div>
<div>
thumb 2
</div>
<div>
thumb 3
</div>
<div>
thumb 4
</div>
</li>
<li>
<div>
thumb 5
</div>
<div>
thumb 6
</div>
<div>
thumb 7
</div>
<div>
thumb 8
</div>
</li>
</ul>
But code is not working for some reason! Looks like after generating few lists the code execution does not stop and browser hangs. Have I used the function in a wrong way: 'query_posts('cat=14&posts_per_page=4&offset='.$pStart)
' ?
How should I actually implement it?
If you've got lots of posts, that outer while
loop is going to keep on going until it's queried every post, 2 at a time!
Seems to me like you're making things complicated for yourself, so here's what I'd do instead;
global $wp_query;
query_posts('cat=14');
if ( have_posts() ):
$last_post = $wp_query->post_count - 1; // index for the last post
$counter = 0;
echo '<ul class= "videoSlider">';
while ( have_posts() ):
the_post();
if ($counter === 0)
echo '<li>';
echo '<div onclick="something()">';
the_post_thumbnail(array(215, 190));
echo '</div>';
if ($counter === 3 || $last_post == $wp_query->current_post) {
$counter = 0;
echo '</li>'; // close the tag every 4th item, or if we're at the end of the loop
} else {
$counter++;
}
endwhile;
echo '</ul>';
endif;
精彩评论