我有一個頁面上的多個循環。第一個循環獲取update
帖子類型的帖子,第二個循環獲取頁面鏈接,第三個獲取post
帖子類型,第四個獲取特定頁面鏈接,然後是最後一個獲取當前頁面數據的循環。即我有類似以下內容:WordPress的 - 頁面上的多個環
獲取更新
<?php $updates = get_posts(array('post_type' => 'update', 'post_per_page' => 4)); ?>
<?php foreach ($updates as $update): ?>
<?php setup_postdata($update); ?>
<li>
<span><?php the_time(get_option('date_format')); ?></span>
<h4><a href="<?php echo $update->guid; ?>"><?php echo get_post_meta($update->ID, 'msp_onhome_title', true); ?></a></h4>
</li>
<?php endforeach ?>
抓取news
頁面鏈接:
<?php
$the_slug = 'news';
$args=array(
'name' => $the_slug,
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$blog_page = get_posts($args);
if($blog_page) :
?>
<span class="stylish-extend">
<a href="<?php echo $blog_page[0]->guid; ?>">More News →</a></span>
<?php endif; ?>
取帖子:
<?php $color_classes = array('dark-blue', 'dark-red', 'black', 'maroone', 'light-blue'); ?>
<?php $posts = get_posts(array('post_type' => 'post', 'post_per_page' => 5)); ?>
<?php foreach ($posts as $post): ?>
<?php setup_postdata($post); ?>
<li>
<article class="blog-post">
<header>
<p class="post-title dark-blue">
<a href="<?php echo $post->guid; ?>"><?php the_title(); ?></a>
<span>Posted in <i><?php the_category(', '); ?></i> by <i><?php the_author(); ?></i></span>
</p>
</header>
</article>
</li>
<!-- <span><?php the_time(get_option('date_format')); ?></span> -->
<?php endforeach ?>
抓取頁面鏈接
<?php
$the_slug = 'blog';
$args=array(
'name' => $the_slug,
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$blog_page = get_posts($args);
if($blog_page) :
?>
<a href="<?php echo $blog_page[0]->guid; ?>">Head to blog »</a>
<?php endif; ?>
獲取實際的頁面內容(當前頁面)
<!-- Problem here -->
<section id="meet" class="light">
<?php if (have_posts()): while(have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</section>
問題就出在這裏,即它並不顯示實際的頁面內容。奇怪的是,當我把這個循環放在最上面的所有循環上時,它會正確顯示頁面數據。我在每個循環之後嘗試過wp_reset_query()
和wp_reset_post_data()
(或者它是wp_reset_postdata()
?),但它不起作用。我試圖另一件事,在可變保存在$wp_query
的參考,所執行的循環,然後恢復$wp_query
即如下所示:
global $wp_query;
$temp_wpquery = $wp_query;
// perform all the loops and stuff
global $wp_query;
$wp_query = $temp_wpquery;
// perform the stuff
仍然結果是相同的。請問任何人都可以告訴我我在這裏做錯了什麼?爲什麼它不顯示最後一個循環中的當前頁面數據?
所以我錯過了'setup_postdata()'嗯。 –
是的,並添加了一個額外的循環;) –