我正在尝试将嵌套循环与posts-to-posts插件结合使用。这两个循环都可以工作,但问题出现在第二个嵌套循环($问题)之后。我想再次访问$publication循环,但数据仍然是$issue数据。
wp_reset_query()
将在单回路中重置回主回路。我不想要的php。
我可以用get_posts()
而不是新的WP\\u查询,但我希望能够使用get_template_part()
.
如何将数据重置回发布循环,以便第二个“发布标题”返回的是发布,而不是问题标题?
这是我的代码。php:
$publication = new WP_Query( array(
\'connected_type\' => \'publication_to_post\',
\'connected_items\' => $post->ID,
\'fields\' => \'ids\',
\'posts_per_page\' => 1,
) );
if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
echo \'<h2>Publication title = \'.get_the_title().\'</h2>\';
$pub_id = get_the_ID();
$issue = new WP_Query( array(
\'connected_type\' => \'publication_to_issue\',
\'connected_items\' => $pub_id,
\'fields\' => \'ids\',
\'posts_per_page\' => 1,
) );
if ( $issue->have_posts() ) {
while ( $issue->have_posts() ) : $issue->the_post();
// need to be able to use template parts in here
echo \'<h2>Issue title = \'.get_the_title().\'</h2>\';
endwhile;
}
// This currently returns the issue title, not the publication title
echo \'<h2>Publication title = \'.get_the_title().\'</h2>\';
endwhile;
}
SO网友:kdev
我将自己回答这个问题,但正是非常聪明的@simonwheatley为人们编写的代码为我解决了这个问题。
而不是使用wp_reset_postdata()
或wp_reset_query()
, 您可以使用以下选项:
$publication->reset_postdata();
其中$publication是您的查询对象。
工作代码现在如下所示:
$publication = new WP_Query( array(
\'connected_type\' => \'publication_to_post\',
\'connected_items\' => $post->ID,
\'fields\' => \'ids\',
\'posts_per_page\' => 1,
) );
if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
echo \'<h2>Publication title = \'.get_the_title().\'</h2>\';
$pub_id = get_the_ID();
$issue = new WP_Query( array(
\'connected_type\' => \'publication_to_issue\',
\'connected_items\' => $pub_id,
\'fields\' => \'ids\',
\'posts_per_page\' => 1,
) );
if ( $issue->have_posts() ) {
while ( $issue->have_posts() ) : $issue->the_post();
// need to be able to use template parts in here
echo \'<h2>Issue title = \'.get_the_title().\'</h2>\';
endwhile; $publication->reset_postdata();
}
echo \'<h2>Publication title = \'.get_the_title().\'</h2>\';
endwhile;
}
SO网友:David
首先,我认为可以使用get_posts()
结合setup_postdata()
. 有了这些,您可以像在普通WordPress循环中一样使用模板标记。
但您也可以在嵌套循环中使用此函数:
# make sure $post is the global in your scope (which should be the case in single.php)
global $post;
if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
echo \'<h2>Publication title = \'.get_the_title().\'</h2>\';
$pub_id = get_the_ID();
# preserve the current post in the higher loop
$preserve_post = get_post();
$issue = new WP_Query( array(
\'connected_type\' => \'publication_to_issue\',
\'connected_items\' => $pub_id,
\'fields\' => \'ids\',
\'posts_per_page\' => 1,
) );
if ( $issue->have_posts() ) {
while ( $issue->have_posts() ) : $issue->the_post();
// need to be able to use template parts in here
echo \'<h2>Issue title = \'.get_the_title().\'</h2>\';
endwhile;
}
# set the global back to your first loop post
$post = $preserve_post;
setup_postdata( $post );
// This currently returns the issue title, not the publication title
echo \'<h2>Publication title = \'.get_the_title().\'</h2>\';
endwhile;
}
wp_reset_query();