使用循环检索 WordPress 帖子
我需要实现一个滑块,它将在每张幻灯片中显示特定类别的 4 个帖子缩略图。为此,我写了这样的:
<ul class= "videoSlider">
<?php
$pStart = 0;
$flag = true;
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();
} ?>
jquery 滑块所需的结构是这样的:
<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>
但代码由于某种原因无法工作!看起来生成几个列表后代码执行不会停止并且浏览器挂起。我是否以错误的方式使用了该函数: 'query_posts('cat=14&posts_per_page=4&offset='.$pStart)
' ? 我应该如何实际实施呢?
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;
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有很多帖子,则外部
while
循环将继续进行,直到查询完每个帖子(一次 2 个)!在我看来,你好像把事情变得复杂了,所以这就是我要做的;
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;