WordPress多重循环
试图在WordPress中构建多个循环,我感到非常沮丧。我看了很多文章——我做错了什么。我将以下内容放在loop.php文件中(因为我已经在此基础上构建了主页)WordPress多重循环,wordpress,loops,Wordpress,Loops,试图在WordPress中构建多个循环,我感到非常沮丧。我看了很多文章——我做错了什么。我将以下内容放在loop.php文件中(因为我已经在此基础上构建了主页) 您不能仅仅发明一个变量“query\u string2”并期望它工作;) 试试这个: <!--Loop 1--> <?php global $query_string; // required $posts = query_posts($query_string.'category_name=news&post
您不能仅仅发明一个变量“query\u string2”并期望它工作;) 试试这个:
<!--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
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
//您的输出html
您已嵌套循环(缺少endwhile)。这将起作用:
<?php
global $query_string; //make sure these are in the correct format for post queries
global $query_string1;
// First loop
$first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');
while ( $first_loop->have_posts() ) : $first_loop->the_post() :
// do your thing
// e.g: $first_loop->the_content();
endwhile;
// second loop
$second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');
while ( $second_loop->have_posts() ) : $second_loop->the_post() :
//do your thing
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
我可以帮忙,但我认为首先,我应该理解一个基本点:你到底想做什么?你的目标是什么?为什么您认为需要嵌套循环
这将帮助我更准确地回答问题。我用下面的代码解决了这个问题,我把它放在一个页面模板中(我的朋友建议最好不要使用loop.php)
有一个名为rewind_posts()的函数,用于回放循环POST,以便在同一页面的不同位置重复使用同一查询
顺便说一句,如果我去掉循环2,它会起作用,似乎不喜欢页面上的2个循环-尽管我使用了reset\u querytry new instant of query\u postsThanks作为您的建议,但不幸的是,它们都不起作用:-(我想你也研究了
endwhile
问题,是吗?像这样在每一个问题之后都加上一个结尾?我试过了,还是不走运-它只是空白。试过了-很抱歉,它不起作用,仍然是空白页。你的$query\u字符串变量里面到底是什么?上面的代码在WP_查询论点。非常感谢你的提议,我已经在朋友的帮助下解决了。现在将发布答案。
<?php
global $query_string; // required
$query_string_backup = $query_string;
$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
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
// your output html
<?php endwhile; ?>
<?php
global $query_string; //make sure these are in the correct format for post queries
global $query_string1;
// First loop
$first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');
while ( $first_loop->have_posts() ) : $first_loop->the_post() :
// do your thing
// e.g: $first_loop->the_content();
endwhile;
// second loop
$second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');
while ( $second_loop->have_posts() ) : $second_loop->the_post() :
//do your thing
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>