我想查询自定义帖子类型cpt_locations 如果office_locations 在灵活的内容字段中选择布局。我肯定我有标准的语法,但如果我查看页面,它会多次打印“是”,然后挂起,就像它无限循环一样。在我的content-page.php 我有:
if( get_row_layout() == \'office_locations\' ):
    $args = array(
     \'post_type\'=> \'cpt_locations\',
     \'posts_per_page\' => 10
    );              
    $locations = new WP_Query( $args );
        if ( $locations -> have_posts() ) {
            while ( $locations -> have_posts() ) {
                echo \'yes\'; 
        }
        wp_reset_postdata();
    }
    wp_reset_query();
endif;
 在中
page.php 我已经陷入了圈套-这就是问题所在吗
<?php
    while ( have_posts() ) :
        the_post();
        get_template_part( \'template-parts/content\', \'page\' );
        if ( comments_open() || get_comments_number() ) :
            comments_template();
        endif;
    endwhile;
?>
 
                    最合适的回答,由SO网友:Jacob Peattie 整理而成
                    你失踪了$locations->the_post() 在您的循环中:
if ( $locations -> have_posts() ) {
    while ( $locations -> have_posts() ) {
        $locations->the_post(); // Needed
        echo \'yes\'; 
    }
}
 此函数所做的一件事是提前
$locations 转到下一个帖子。没有它
$locations->have_posts() 只会无限重复第一篇帖子。