[英]Multiple Loops / WP_Query on One Page
我尝试在archive-custom.php上有两个循环(用于自定义帖子类型)-一个循环用于精选帖子,另一个循环用于其余帖子。
这是我想出的代码,但是无法正常工作。 目前,它既不显示任何循环,也实际上导致破坏了其他基于PHP的元素。
注意:这些循环分为不同的模板部分-不确定是否重要。 但是,我将它们合并为一个块,以便更轻松地进行故障排除。
<?php $args = array (
'post_type' => 'community',
'category_name' => 'featured',);
// The Query
$community_posts_featured = new WP_Query( $args );
if ($community_posts_featured->have_posts()) : while ($community_posts_featured->have_posts()) : $community_posts_featured->the_post(); ?>
<div id="featured">
<--Featured Stuff Here-->
<?php the_content(); ?>
</div><!--End #featured-->
<?php endwhile; ?>
<?php $args = array (
'post_type' => 'community', );
// The Query
$community_posts = new WP_Query( $args );
if ($community_posts->have_posts()) : while ($community_posts->have_posts()) : $community_posts->the_post(); ?>
<div id="main-content">
<--Main Stuff Here-->
<?php the_content(); ?>
</div><!--#End Main-->
<?php endwhile; ?>
<?php else : ?>
<--Missing Content Stuff-->
<?php endif; ?>
在那里,我可以发现两个问题:1)您已经打开了2个if语句,并且刚刚关闭了其中之一。2)您最好使用wp_reset_query();
在第一个循环之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.