2017-07-07 119 views
2

我試圖在一個後循環之前和之後得到一個頁面ID,頁面ID在循環之前正確顯示,但是當我嘗試在循環後顯示它時,它只顯示最後一個帖子ID的循環。重置WordPress後循環

我已經嘗試使用以下所有重置循環,沒有似乎工作:

wp_reset_postdata() 
wp_reset_query() 
rewind_posts() 

的循環代碼:

<?php 

echo 'Shows the page ID (correct)=' . $post->ID; 

$args = array(
'post_type' => 'accommodation', 
'posts_per_page' => '9999', 
); 

$wp_query = new WP_Query($args); 
if ($wp_query->have_posts()) : 
while($wp_query->have_posts()) :  
$wp_query->the_post(); ?> 

<option value="<?php the_ID(); ?>"><?php echo the_title(); ?></option> 

<?php endwhile; 
else : 
esc_html_e('No bookings','sohohotel'); 
endif; 

wp_reset_postdata(); 

echo 'Shows the ID of the last post in the loop (not correct)=' . $post->ID; ?> 

注意我使用WP_Query而不是query_posts,所以我不知道我要去哪裏錯,任何想法非常感謝!

+0

和一個簡單的'$ letsStoreThisValueBecauseWeWillNeedItLater = $後> ID;'不能夠解決你的問題......? – CBroe

+0

你可以將ID保存在一個變量中。所以,你可以在使用前後 – user2584538

+0

謝謝!爲什麼我沒有想到...有時最好的解決方案是最簡單的 –

回答

1

使用本

$wp_query = new WP_Query($args); 
if ($wp_query->have_posts()) : $wp_query->the_post(); 
while($wp_query->have_posts()) :  
$wp_query->the_post(); ?> 

<option value="<?php the_ID(); ?>"><?php the_title(); ?></option> 

<?php endwhile; ?> 
<?php wp_reset_postdata(); ?> 
<?php else : 
esc_html_e('No bookings','sohohotel'); 
endif; 
+0

謝謝,但不會有任何區別 –

+0

嘗試編輯ans @TheBobster – Exprator