Почему мой компонент, находящийся за пределами foreach l oop, проходит через петлю? - PullRequest
1 голос
/ 25 апреля 2020

Почему мой компонент, который находится за пределами foreach l oop, проходит через петлю? У меня есть foreach l oop, который получает все сообщения определенной категории. L oop правильно отображает все мои сообщения в этой категории.

Проблема, с которой я сталкиваюсь, заключается в том, что компонент (который имеет свой собственный, в то время как l oop - повторитель ACF) показывает содержимое компонента для последней отображаемой дочерней записи.

Пример:

Структура

  • Конкретные примеры с содержанием компонента Panel 0 (родительский элемент)

    • Тематическое исследование 1 - с содержанием компонента Panel 1 (ребенок)

    • Тематическое исследование 2 - с содержанием компонента Panel 2 (ребенок)

    • Пример 3 - с компонентом панели Содержание 3 (дочерний элемент)

Значение содержимого «3» будет отображаться на Страница Case Study (Parent) вместо значения «0».

Я пытался заняться этим весь день и не могу понять, что происходит. Любая помощь приветствуется.

Компонент панели:

<!-- Panels Component: w/ ACF repeater loop -->

<section class="two-col-grid-panel">
<?php
    // check if the repeater field has rows of data
    if( have_rows('panels') ):
        // loop through the rows of data
        while ( have_rows('panels') ) : the_row(); ?>
            <div class="panel">
                <h2><?php the_sub_field('panel_heading'); ?></h2>
                <p><?php the_sub_field('panel_description'); ?></p>
                <a href="<?php the_sub_field('button_link'); ?>" class="small-card__btn"><?php the_sub_field('button_text'); ?></a>
            </div>
        <?php endwhile;
    endif;
?>
</section>

Пользовательский шаблон (родительский): с foreach l oop

<main>
    <section id="hero-container" class="single-col-card-container">
        <div class="small-card-container">
            <div class="studies-grid-container">
                <?php
                $args  = array(
                    'cat' => 6,
                    'orderby'         => 'post_date',
                    'order'           => 'ASC'
                ); 
                $posts = get_posts($args);
                foreach ($posts as $post) : ?>
                    <div class="grid__item">
                        <div class="card">
                            <!-- Retrieve the image and alt text in repeater -->
                            <img class="small-card__img" src="http://localhost/master/wp-content/uploads/2020/04/product-design.jpg" alt="Product Design">
                            <div class="small-card__content">
                                <h2 class="small-card__header"><?php the_title(); ?></h2>
                                <p class="small-card__text"><?php the_excerpt(); ?></p>
                                <a href="<?php the_permalink(); ?>" class="small-card__btn">Read More</a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div> 
    </section>

    <?php get_template_part('component', 'panels'); ?>
</main>

Пользовательский дочерний шаблон

<main>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <section class="browser-hero-container">
            <div class="browser-hero">
                <?php 
                    $imageID = get_field('browser_hero_image');
                    $image = wp_get_attachment_image_src( $imageID, 'full' );
                    $alt = get_post_meta($imageID , '_wp_attachment_image_alt', true);
                ?>
                <img src="<?php the_field('browser_hero_image'); ?>" alt="<?php echo $alt; ?>" />
            </div>
        </section>

        <section class="three-col-grid-block-container">
            <?php
                // check if the repeater field has rows of data
                if( have_rows('case_study_tiles') ):
                    // loop through the rows of data
                    while ( have_rows('case_study_tiles') ) : the_row(); ?>
                        <div class="grid-block-item">
                            <h3><?php the_sub_field('tile_title'); ?></h3>
                            <p><?php the_sub_field('tile_description'); ?></p>
                        </div>
                    <?php endwhile;
                endif;
            ?>
        </section>

        <?php get_template_part('component', 'panels'); ?>

        <?php //if ( ! post_password_required() ) comments_template( '', true ); ?>
    <?php endwhile; endif; ?>
</main>

var_dump

array (size=5)
  0 => 
    object(WP_Post)[2099]
      public 'ID' => int 347
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2020-04-19 15:35:05' (length=19)
      public 'post_date_gmt' => string '2020-04-19 15:35:05' (length=19)
      public 'post_content' => string '<!-- wp:paragraph -->
        <p>Look up at the night sky, and find yourself immersed in the amazing mountain range of Aspen.</p>
        <!-- /wp:paragraph -->' (length=144)
      public 'post_title' => string 'Case Study 1' (length=12)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'open' (length=4)
      public 'ping_status' => string 'open' (length=4)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'case-study-1' (length=12)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2020-04-19 16:12:33' (length=19)
      public 'post_modified_gmt' => string '2020-04-19 16:12:33' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://localhost/master/?p=347' (length=30)
      public 'menu_order' => int 0
      public 'post_type' => string 'post' (length=4)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
  1 => 
    object(WP_Post)[1742]
      public 'ID' => int 352
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2020-04-19 16:13:31' (length=19)
      public 'post_date_gmt' => string '2020-04-19 16:13:31' (length=19)
      public 'post_content' => string '<!-- wp:paragraph -->
        <p>Look up at the night sky, and find yourself immersed in the amazing mountain range of Aspen.2</p>
        <!-- /wp:paragraph -->' (length=145)
      public 'post_title' => string 'Case Study 2' (length=12)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'open' (length=4)
      public 'ping_status' => string 'open' (length=4)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'case-study-2' (length=12)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2020-04-19 16:53:19' (length=19)
      public 'post_modified_gmt' => string '2020-04-19 16:53:19' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://localhost/master/?p=352' (length=30)
      public 'menu_order' => int 0
      public 'post_type' => string 'post' (length=4)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
  2 => 
    object(WP_Post)[2097]
      public 'ID' => int 356
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2020-04-19 16:46:24' (length=19)
      public 'post_date_gmt' => string '2020-04-19 16:46:24' (length=19)
      public 'post_content' => string '<!-- wp:paragraph -->
        <p>Look up at the night sky, and find yourself immersed in the amazing mountain range of Aspen.3</p>
        <!-- /wp:paragraph -->' (length=145)
      public 'post_title' => string 'Case Study 3' (length=12)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'open' (length=4)
      public 'ping_status' => string 'open' (length=4)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'case-study-3' (length=12)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2020-04-24 03:14:02' (length=19)
      public 'post_modified_gmt' => string '2020-04-24 03:14:02' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://localhost/master/?p=356' (length=30)
      public 'menu_order' => int 0
      public 'post_type' => string 'post' (length=4)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
  3 => 
    object(WP_Post)[2096]
      public 'ID' => int 359
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2020-04-19 16:47:09' (length=19)
      public 'post_date_gmt' => string '2020-04-19 16:47:09' (length=19)
      public 'post_content' => string '<!-- wp:paragraph -->
        <p>Look up at the night sky, and find yourself immersed in the amazing mountain range of Aspen.4</p>
        <!-- /wp:paragraph -->' (length=145)
      public 'post_title' => string 'Case Study 4' (length=12)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'open' (length=4)
      public 'ping_status' => string 'open' (length=4)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'case-study-4' (length=12)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2020-04-19 16:53:09' (length=19)
      public 'post_modified_gmt' => string '2020-04-19 16:53:09' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://localhost/master/?p=359' (length=30)
      public 'menu_order' => int 0
      public 'post_type' => string 'post' (length=4)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
  4 => 
    object(WP_Post)[1938]
      public 'ID' => int 364
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2020-04-19 20:15:07' (length=19)
      public 'post_date_gmt' => string '2020-04-19 20:15:07' (length=19)
      public 'post_content' => string '<!-- wp:paragraph -->
        <p>Look up at the night sky, and find yourself immersed in the amazing mountain range of Aspen.5</p>
        <!-- /wp:paragraph -->' (length=145)
      public 'post_title' => string 'Case Study 5' (length=12)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'open' (length=4)
      public 'ping_status' => string 'open' (length=4)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'case-study-5' (length=12)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2020-04-27 13:33:56' (length=19)
      public 'post_modified_gmt' => string '2020-04-27 13:33:56' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://localhost/master/?p=364' (length=30)
      public 'menu_order' => int 0
      public 'post_type' => string 'post' (length=4)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...