問題描述
我試圖使用嵌套循環與帖子來發布插件。循環都有效,但問題出現在第二個嵌套循環 ($ issue) 之後。我想再次訪問 $ publication 循環,但數據仍然是 $ issue 數據。
wp_reset_query()
將重新設置為 single.php 中不需要的主循環。
我可以使用 get_posts()
而不是新的 WP_Query,但我想要能夠使用 get_template_part()
。
如何將數據重新設置回發佈循環,以便第二個’Publication title’ 返回發佈,而不是問題的標題?
這是我在 one.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;
}
最佳解決方案
我會自己回答這個問題,但是這是為我解決這個問題的人民法典中非常聰明的 @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;
}
次佳解決方案
首先,我認為可以使用 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();
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。