[英]Wordpress - Multiple loops on a page
I have got multiple loops on a page. 我在页面上有多个循环。 First loop gets the posts from
update
post type, second loop gets a page link, third one gets post
post type, again fourth one gets a specific page link and then there is the last loop that fetches the current page data. 第一圈会从帖子
update
后的类型,第二个循环得到一个页面链接,第三个获得post
岗位类型,再次第四个获得一个特定的网页链接,然后还有就是抓取当前页面数据最后的循环。 ie I have got something like the following: 即我有类似以下内容:
Fetch the updates 获取更新
<?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 ?>
Fetch the news
page link: 获取
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; ?>
Fetch posts: 获取帖子:
<?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 ?>
Fetch Page link 提取页面链接
<?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; ?>
Fetch the actual page content (current page) 获取实际页面内容(当前页面)
<!-- Problem here -->
<section id="meet" class="light">
<?php if (have_posts()): while( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</section>
The problem lies here ie it doesn't show the actual page content. 问题出在这里,即它没有显示实际的页面内容。 Strangely, when I take this loop above all the loops at the top most position, it shows the page data correctly.
奇怪的是,当我将此循环置于所有循环的最上方位置时,它正确显示了页面数据。 I have tried
wp_reset_query()
and wp_reset_post_data()
(or was it wp_reset_postdata()
?) after each of the loop but it didn't work. 在每个循环之后,我都尝试过
wp_reset_query()
和wp_reset_post_data()
(或者是wp_reset_postdata()
吗?),但是没有用。 Another thing I tried is, saved the reference of the $wp_query
in a variable, performed the loops and then restored the $wp_query
ie like the following: 我尝试过的另一件事是,将
$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
Still the results were same. 结果仍然相同。 Can any body please tell me what am I doing wrong here?
有人可以告诉我我在做什么错吗? Why doesn't it show the current page data in the last loop?
为什么在最后一个循环中不显示当前页面数据?
You'll need to use the variable name $post
specifically (not another variable name) in setup_postdata()
otherwise it wont work. 您需要在
setup_postdata()
专门使用变量名$post
(而不是其他变量名),否则它将无法工作。
try to change the code like this: 尝试像这样更改代码:
<?php $updates = get_posts(array('post_type' => 'update', 'post_per_page' => 4)); ?>
<?php foreach ($updates as $post): ?>
<?php setup_postdata($post); ?>
<li>
<span><?php the_time(get_option('date_format')); ?></span>
<h4><a href="<?php echo $post->guid; ?>"><?php echo get_post_meta($post->ID, 'msp_onhome_title', true); ?></a></h4>
</li>
<?php wp_reset_query(); endforeach ?>
Depending on where you are using setup_postdata()
(if it is not in the main loop, or in a function/sidebar widget, for example), you may also need to declare - global $post;
根据您使用
setup_postdata()
(例如,如果它不在主循环或函数/边栏小部件中),则可能还需要声明- global $post;
. 。
You don't need to save reference for $wp_query
instead save the reference of global $post;
您无需保存
$wp_query
的引用,而是保存global $post;
的引用global $post;
At the very top after get_header()
在
get_header()
之后的最顶端
Insert this piece of code: 插入这段代码:
global $post;
$originalpost = $post;
//.... Then
//All your awesome stuff here
//...
//Before the actual page contents
<?php $post = $originalpost; ?>
<section id="meet" class="light">
<?php if (have_posts()): while( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</section>
The following has worked for us when conditionally including various PHP files that have sub-loops which predicated on post tags or other meta data of the main post for that page. 当有条件地包含具有子循环的各种PHP文件时,以下内容对我们有用,这些子循环基于该页面的主要帖子的帖子标签或其他元数据。 As one of the previous responses suggested, including wp_reset_postdata() is necessary.
正如前面的建议之一,包括wp_reset_postdata()是必要的。 Does read like you should at least be running wp_reset_postdata() immediately before the last content loop based on your current design.
根据您当前的设计,确实读起来像您应该至少在上一个内容循环之前立即运行wp_reset_postdata()一样。
Case 1: simply reference the global $wp_query variable. 情况1:仅引用全局$ wp_query变量。
global $wp_query;
if (have_posts()) {
while( have_posts() ) :
$wp_query->the_post();
the_content();
endwhile;
}
wp_reset_postdata();
Case 2: running additional query 情况2:运行其他查询
$the_query = new WP_Query( 'page_id=1369' );
while ( $the_query->have_posts() ) :
$the_query->the_post();
the_content();
endwhile;
wp_reset_postdata();
What worked for me is, storing the $post
in some variable, performing all the stuff, assigning stored $post
value back to $post
and after that I used setup_postdata($post)
and then, without using any loop. 对我有用的是,将
$post
存储在某个变量中,执行所有操作,将存储的$post
值分配回$post
,然后使用setup_postdata($post)
,然后不使用任何循环。 And that's it. 就是这样。 Following is the sample code:
以下是示例代码:
global $post;
$temp_post = $post;
//.....
// Every thing else
//...
//Before the actual page content
<?php $post = $temp_post; ?>
<section id="meet" class="light">
<?php setup_postdata($post); ?>
<?php the_content(); ?>
</section>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.