我使用某个Wordpress集成软件的主题,这意味着我基本上坚持使用模板层次结构,否则我只是设置一个自定义模板来解决这个问题。
无论如何,我有我的page.php,看起来有点像下面的
<?php //start the main loop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( is_page('contact-us') ) {
get_template_part( 'content', 'contact' );
}else{
get_template_part( 'content', 'page' );
}
?>
<?php //end the main loop
endwhile; else: ?>
Something is missing
<?php endif; ?>
这很好用,正如预期的那样,我可以在content-page.php
中添加html但是,我想在content-page.php中添加一个自定义循环来显示客户推荐。我使用下面的代码在content-page.php:
中尝试了这个<?php //close the main loop
endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php
//close the main loop and open a custom loop
wp_reset_postdata();
$args = array (
'post_type' => 'testimonial',
'posts_per_page' => 5,
'orderby' => 'rand'
);
$the_query = new WP_Query ( $args );
if ( have_posts() ) : while ( $the_query ->have_posts() ) : $the_query ->the_post(); ?>
Do HTML stuff here
<?php //close the custom loop
endwhile; else: ?>
Uh oh, there is meant to be a testimonial here. Please create some.
<?php endif; ?>
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
此代码会创建一个PHP错误(我尝试关闭主循环时意外结束)。但是,我把这个完全相同的代码直接放在page.php中,它可以工作。只有在content-page.php中才会出错。我无法使用get_template_part包含自定义循环吗?
答案 0 :(得分:0)
最后endwhile和endif没有关闭
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) : while ( have_posts() ) : the_post();
**endwhile; endif;**
?>