WP_QUERY在嵌套循环中中断循环

时间:2020-11-02 作者:Miraj Aryal

我无法找出以下代码的解决方案。当我删除查询部分时,location循环,但使用代码,只显示一个location结果(父循环)。

    if (have_rows("locations")):
    while (have_rows(\'locations\')): the_row();
        ?>
    <section class="content container pt-0">
    <div class="contain-container">
        <div class="row">
            <div class="col-xs-12 col-lg-12" data-aos="fade-up">
                <?php

                <div class="displayaddress">
                    <div class="col-lg-6 col-sm-12">
                        <div class="p-lg max-conten">
                            <?php echo get_sub_field("content_text"); ?> </div>
                        <?php
        $link = get_sub_field(\'content_button_link\');
        if ($link) {
            $link_url = get_sub_field(\'content_button_link\');
        } else {
            $link_url = "#";
        }?>
                        <div class="info">
                            <p class="st3">Address</p>
                            <p class="p-md"><?php echo get_sub_field(\'address\'); ?></p>
                            <a href="<?php $link_url;?>" class="gold-link grey">GET DIRECTIONS →</a>
                        </div>
                        <div class="info">
                            <p class="st3">Phone</p>
                            <p class="p-lg"><?php echo get_sub_field(\'phone\'); ?></p>
                        </div>
                        <div class="info">
                            <p class="st3">Opening Hours</p>
                            <p class="p-lg"><?php echo get_sub_field(\'opening_hours\'); ?></p>
                        </div>
                    </div>
                    <div class="col-lg-6 text-right col-sm-12">
                        <?php echo get_sub_field(\'map_iframe\'); ?>
                    </div>
                </div>

                <?php $featured_posts = get_sub_field(\'designs_on_display\');
        if ($featured_posts) {
            ?>
                <p class="mid-gold st2 bold container dod-title">Designs on Display</p>
                <div class="designondisplay">
                    <?php foreach ($featured_posts as $featured_post) {
                $args = array(
                    \'p\' => $featured_post,
                    \'post_type\' => \'any\',
                    \'post_parent\' => 0,
                );
                $query = new WP_Query($args);
                while ($query->have_posts()): $query->the_post();
                    $termsBath = get_the_terms($post->ID, \'bathrooms\');
                    $termsBath = array_shift($termsBath);
                    $storey = get_the_terms($post->ID, \'storeys\');
                    $storey = array_shift($storey);
                    $bedroom = get_the_terms($post->ID, \'bedrooms\');
                    $bedroom = array_shift($bedroom);
                    ?>
                    <div class="col-sm-12 col-md-6 col-lg-3 p-0" data-aos="fade-up">
                        <div class="project-grid ">
                            <a href=" <?php echo get_permalink(); ?>">
                                <?php
            while (have_rows(\'home_images\')): the_row();?>

                                <?php
                $image = $image_url[0];?>
                                <img src="<?php echo get_sub_field(\'image\')[\'url\'] ?>"
                                    alt="<?php echo get_the_title(); ?>" />
                                <?php

                        break;
                    endwhile;
                    ?>

                                <div class="projectMeta">
                                        <div class="projectIcons">
                                        <h5 class="p-lg gold-9b"><?php echo get_the_title(); ?>
                                        </h5>
                                        <?php
           $bedroom = $bedroom->name;
                    echo \'<span class="icon-number"><img class="bedIcon" src="\' . get_template_directory_uri() . \'/images/[email protected]" />\';
                    echo str_replace("Bedrooms", "", $bedroom) . \'</span>\';

                    if (!empty($termsBath)) {?><span class="icon-number"><?php
            echo \'<img class="bathIcon" src="\' . get_template_directory_uri() . \'/images/[email protected]" />\';

                        $bathroom = $termsBath->name;
                        echo str_replace("Bathroom", "", str_replace("Bathrooms", "", $bathroom));
                        echo "</span>";
                    }
                    ?>
                                    </div>
                                </div>
                            </a>
                        </div>
                    </div>
                    <?php endwhile;}
        }
        ?>
                </div>
            </div>

        </div>
    </div>
</section>
<?php

    endwhile;
endif;
?>

1 个回复
SO网友:Tim

使用后WP_Query 在自定义循环中,可能需要重置全局查询对象。尝试调用wp_reset_query() 在您的while

https://developer.wordpress.org/reference/functions/wp_reset_query/

相关推荐

do_action not working in loop

我一直在使用do\\u action在循环中生成多个动作挂钩。对我来说,do\\u操作不起作用似乎很不幸。function ltc_generate_input_fields($fields = array()) { echo \"Hello World\"; if (empty($fields) || !is_array($fields)) return; foreach($fields as $field) { &#