简体   繁体   English

使用PHP从ACF中提取嵌套的转发器内容-关闭标签的问题

[英]Pulling nested repeater content from ACF with PHP — issue with closing tags

I'm a newbie to php and am having a hard time spotting where the closing tags (mostly divs) need to be placed within this PHP to match the original HTML I coded without dynamic content (for a Wordpress site). 我是php的新手,很难找出需要在此PHP中放置结束标记(主要是div)以匹配我在没有动态内容的情况下(对于Wordpress网站)编码的原始HTML。 Right now my footer (not shown below) is getting pushed around because there's too many/not enough and the elements aren't nesting properly. 现在,我的页脚(未在下面显示)被推挤了,因为太多/不足,并且元素没有正确嵌套。

The function of the code is to pull information from nested repeaters in Advanced Custom Fields, loop through each element and place it into the html structure. 该代码的功能是从“高级自定义字段”中的嵌套转发器中提取信息,循环遍历每个元素并将其放入html结构中。

PHP: PHP:

<?php

if (have_rows('pSect')):
    while (have_rows('pSect')):
        the_row();
        $productSectionTitle = get_sub_field('pSecTitle');
        echo '<div class="block_title cf" id="collectie_block_title">' . $productSectionTitle . '</div>';

        if (have_rows('prods')):
            while (have_rows('prods')):
                the_row();
                $products = get_sub_field('prods');
                echo '<div class="block cf" id="item_block">';

                if (have_rows('indivProd')):
                    while (have_rows('indivProd')):
                        the_row();
                        $individualProduct = get_sub_field('indivProd');
                        $images            = get_sub_field('images');

                        if ($images):
                            foreach ($images as $image):
                                $full_image_url = $image['url'];
                                echo '<div class="item_block_left bstretchMe cf" data-img-src="' . $full_image_url . '"></div>';
                            endforeach;
                        endif;

                        $productName = get_sub_field('product_name');
                        $productType = get_sub_field('product_type');


                        echo '<div class="item_block_right cf">' . '<div class="item_block_right_header cf">' . '<ul class="item_block_right_header_list">' . '<li id="product_title">' . $productName . '</li>' . '<li id="product_subtitle">' . $productType . '</li>' . '</ul>' . '<div class="item_block_right_viewoptions">bestellen opties</div>' . '</div>' . '<div class="item_block_right_details cf">' . '<div class="item_block_right_details_specs">' . '<h5 class="item_block_right_details_specstitle">Lorem Ipsum</h5>' . '<ul class="item_block_right_details_specslist">';

                        if (have_rows('detailList')):
                            while (have_rows('detailList')):
                                the_row();
                                $bijzonderheden = get_sub_field('bijzonderheden');
                                $message        = "working!?";
                                echo '<li>' . $bijzonderheden . '</li>';
                            endwhile;
                        endif;

                        echo '</ul>' . '</div>' . '<div class="item_block_right_details_kleuren cf">' . '<p id="item_block_right_details_kleuren_title">Kleuren</p>';

                        if (have_rows('colOps')):
                            while (have_rows('colOps')):
                                the_row();
                                $colorPick = get_sub_field('cPick');
                                echo '<div id="item_block_right_details_kleuren_swatches" style="background-color:' . $colorPick . ';"></div>';
                            endwhile;
                        endif;

                        echo '</div>' . '<div class="item_block_right_details_ordering">' . '<h5 class="item_block_right_details_orderingtitle">Lorem Ipsum</h5>' . '<p class="item_block_right_details_orderingp">' . 'All products created through DITT Bags are custom made. Details such as color, size and detailing will be discussed upon the beginning of a new project. To order a bag and begin a new project, send an inquiry to  inquiries@dittbags.com' . '</p>' . '</div>' . '</div>';
                    endwhile;
                endif;

                echo '</div>' . 
                    '</div>'; 
            endwhile;
        endif;

        echo '</div>'; 
    endwhile;
endif;

echo '</div>' . '</div>';

?>

ORIGINAL HTML: 原始HTML:

<div class="block_title cf" id="collectie_block_title">Tassen</div>

<div class="block cf" id="item_block">
  <div class="item_block_left cf">
    img img img make this a backstretch slideshow that autoplays when     item is selected
  </div>
  <div class="item_block_right cf">
    <div class="item_block_right_header cf">
      <ul class="item_block_right_header_list">
        <li id="product_title">SANNE</li>
        <li id="product_subtitle">leren backpack XL</li>
      </ul>
      <div class="item_block_right_viewoptions">bestellen opties</div>
    </div>
    <div class="item_block_right_details cf">
      <div class="item_block_right_details_specs">
        <h5 class="item_block_right_details_specstitle">Lorem     Ipsum</h5>
        <ul class="item_block_right_details_specslist">
          <li>lorem ipsum</li>
          <li>lorem ipsum</li>
          <li>lorem ipsum</li>
        </ul>
      </div>
      <div class="item_block_right_details_kleuren">
        <p id="item_block_right_details_kleuren_title">Kleuren</p>
        <div     id="item_block_right_details_kleuren_swatches">,.,.,.,.,.,.,</div>
      </div>
      <div class="item_block_right_details_ordering">
        <h5 class="item_block_right_details_orderingtitle">Lorem Ipsum</h5>
    <p class="item_block_right_details_orderingp">
      All products created through DITT Bags are custom made. Details such as color, size and detailing will be discussed upon the beginning of a new project. To order a bag and begin a new project, send an inquiry to inquiries@dittbags.com
    </p>
  </div>
</div>

Thanks all! 谢谢大家!

Opening and closing elements in one line as an echo is going to cause you all kinds of issues. 在一行中打开和关闭元素作为回声将导致您遇到各种问题。 This line is the main one I'd avoid (which is where I think the error was. 这行是我要避免的主要行(这是我认为错误所在的位置。

echo '</div>' . '<div class="item_block_right_details_ordering">' . '<h5 class="item_block_right_details_orderingtitle">Lorem Ipsum</h5>' . '<p class="item_block_right_details_orderingp">' . 'All products created through DITT Bags are custom made. Details such as color, size and detailing will be discussed upon the beginning of a new project. To order a bag and begin a new project, send an inquiry to inquiries@dittbags.com' . '</p>' . '</div>' . '</div>';

There is definitely personal preferences in the code below but a couple of key points think about are below: 下面的代码中肯定有个人喜好,但以下几点要考虑:

You'll use two types of PHP files. 您将使用两种类型的PHP文件。 Ones for views (where you're rendering HTML) and ones where you just have logic. 一个用于视图(用于呈现HTML),另一个用于仅具有逻辑的视图。 You've written yours in more of a logic style way where everything is wrapped in one massive PHP tag. 您已经以一种逻辑样式的方式编写了自己的代码,其中所有内容都包裹在一个庞大的PHP标签中。

If you're rendering a view, I would recommend opening and closing PHP round each line of logic. 如果要渲染视图,建议您围绕逻辑的每一行打开和关闭PHP。

This helps read the file as a view easier. 这有助于更轻松地读取文件。 I know there's a lot of opening and closing but when you look at it I think you'll agree it's easier to interpret. 我知道有很多打开和关闭的内容,但是当您查看它时,我认为您会同意它更易于解释。

Most important point - Indentation - Doing the above means that you can keep your opening and closing tags indented correctly and will make it much easier to see where elements open and close. 最重要的一点-缩进 -执行上述操作意味着您可以使开始和结束标签正确缩进,并使查看元素在何处打开和关闭更加容易。

Here's how I would write this file. 这是我编写此文件的方式。 I haven't tested this file as I don't have the data to render with it but it shows you the concept and the indentation gives me confidence it would be correct. 我没有测试过该文件,因为我没有可用它呈现的数据,但是它向您展示了概念,并且缩进使我确信它是正确的。

<?php if (have_rows('pSect')): ?>
    <?php while (have_rows('pSect')) : ?>
        <?php the_row(); ?>
        <?php $productSectionTitle = get_sub_field('pSecTitle'); ?>
        <div class="block_title cf" id="collectie_block_title"><?php echo $productSectionTitle; ?></div>

        <?php if (have_rows('prods')) : ?>
            <?php while (have_rows('prods')): ?>
                <?php the_row(); ?>
                <?php $products = get_sub_field('prods'); ?>
                <div class="block cf" id="item_block">
                    <?php if (have_rows('indivProd')): ?>
                        <?php while (have_rows('indivProd')): ?>
                            <?php the_row(); ?>
                            <?php
                                $individualProduct = get_sub_field('indivProd');
                                $images            = get_sub_field('images');
                            ?>

                            <?php if ($images): ?>
                                <?php foreach ($images as $image): ?>
                                    <?php $full_image_url = $image['url']; ?>
                                        <div class="item_block_left bstretchMe cf" data-img-src="<?php echo $full_image_url; ?>"></div>
                                <?php endforeach; ?>
                            <?php endif; ?>

                            <?php
                                $productName = get_sub_field('product_name');
                                $productType = get_sub_field('product_type');
                            ?>


                            <div class="item_block_right cf">
                                <div class="item_block_right_header cf">
                                    <ul class="item_block_right_header_list">
                                        <li id="product_title"><?php echo $productName; ?></li>
                                        <li id="product_subtitle"><?php $productType; ?></li>
                                    </ul>
                                </div>
                                <div class="item_block_right_viewoptions">bestellen opties</div>
                            </div>

                            <div class="item_block_right_details cf">
                                <div class="item_block_right_details_specs">
                                    <h5 class="item_block_right_details_specstitle">Lorem Ipsum</h5>
                                        <ul class="item_block_right_details_specslist">

                                            <?php if (have_rows('detailList')): ?>
                                                <?php while (have_rows('detailList')): ?>
                                                    <?php the_row(); ?>
                                                        <?php
                                                            $bijzonderheden = get_sub_field('bijzonderheden');
                                                            $message        = "working!?";
                                                        ?>
                                                        <li><?php echo $bijzonderheden; ?></li>;
                                                <?php endwhile; ?>
                                            <?php endif; ?>

                                        </ul>
                                    </div>
                                <div class="item_block_right_details_kleuren cf">
                                    <p id="item_block_right_details_kleuren_title">Kleuren</p>

                                    <?php if (have_rows('colOps')): ?>
                                        <?php while (have_rows('colOps')): ?>
                                            <?php the_row(); ?>
                                            <?php $colorPick = get_sub_field('cPick'); ?>
                                            <div id="item_block_right_details_kleuren_swatches" style="background-color:<?php echo $colorPick; ?>"></div>
                                        <?php endwhile; ?>
                                    <?php endif; ?>
                                </div>

                                <div class="item_block_right_details_ordering">
                                    <h5 class="item_block_right_details_orderingtitle">Lorem Ipsum</h5>
                                    <p class="item_block_right_details_orderingp">
                                        All products created through DITT Bags are custom made. Details such as color, size and detailing will be discussed upon the beginning of a new project. To order a bag and begin a new project, send an inquiry to  inquiries@dittbags.com
                                    </p>
                                </div>
                            </div>
                        <?php endwhile; ?>
                    <?php endif; ?>
                </div>
            <?php endwhile; ?>
        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?>

The next thing you should be doing is breaking this file up into multiple files to make it more manageable. 您下一步应该做的就是将该文件分成多个文件,以使其更易于管理。 You can then use PHP to include the different bits that make up the bigger file. 然后,您可以使用PHP包含组成更大文件的不同位。

You can use include to do this. 您可以使用include来做到这一点。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM