WordPress 多循环
我在尝试在 WordPress 中构建多个循环时感到非常沮丧。我看了很多文章 - 我做错了什么。我将以下内容放入了loop.php 文件中(因为我已经在此基础上构建了主页)...
<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
I am getting horribly frustrated trying to build multiple loops in WordPress. I've looked at loads of articles - what am I doing wrong. I placed the following in the loop.php file (because I've built the homepage on this)...
<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您已经嵌套了循环(缺少 endwhile)。
You have nested the loops (missing endwhile).
您不能只是发明一个变量“query_string2”并期望它起作用;)
试试这个:
如果这不起作用(我不记得
wp_reset_query()
是否重置了$query_string< /code> global 超出了我的想象),请尝试以下操作:
You cannot just invent a variable "query_string2" and expect it to work ;)
Try this:
If that doesn't work (I don't remember whether
wp_reset_query()
resets the$query_string
global off the top of my head), try the following:这会起作用:
This will work:
我可以帮忙,但我认为首先,我应该了解一个基本原则:你到底想做什么?你的目标是什么?为什么你认为你需要嵌套循环?
这将帮助我更正确地提问。
I could help out, but I think that first, I should understand 1 fundamental: WHAT are you trying to do exactly? What is your goal? Why do you think you need nested loops?
This will help me aswering more correctly.
我用下面的代码解决了这个问题,我将其放入页面模板中(我的朋友建议最好保留loop.php)
I solved it with the following code, which I put in a page template (my friend advised that it's best leaving the loop.php alone)
有一个名为 rewind_posts() 的函数,它可以倒回循环帖子,以便您可以在同一页面的不同位置重复使用相同的查询
https://codex.wordpress.org/Function_Reference/rewind_posts
There's a function called rewind_posts() that rewinds the loop posts in order to let you re-use the same query in different locations on the same page
https://codex.wordpress.org/Function_Reference/rewind_posts