【问题标题】:Pulling nested repeater content from ACF with PHP — issue with closing tags使用 PHP 从 ACF 中提取嵌套的转发器内容 - 结束标签的问题
【发布时间】:2018-12-22 02:30:24
【问题描述】:

我是 php 的新手,我很难发现结束标签(主要是 div)需要放置在这个 PHP 中的哪个位置,以匹配我在没有动态内容的情况下编码的原始 HTML(对于 Wordpress 网站)。现在我的页脚(未在下面显示)被推来推去,因为太多/不够,并且元素没有正确嵌套。

代码的功能是从高级自定义字段中的嵌套转发器中提取信息,循环遍历每个元素并将其放入 html 结构中。

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>';

?>

原始 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>

谢谢大家!

【问题讨论】:

  • 你的输出 HTML 是什么样的?

标签: php wordpress nested-loops advanced-custom-fields


【解决方案1】:

在一行中打开和关闭元素作为回显会给你带来各种各样的问题。这一行是我要避免的主要行(这是我认为错误所在。

echo '&lt;/div&gt;' . '&lt;div class="item_block_right_details_ordering"&gt;' . '&lt;h5 class="item_block_right_details_orderingtitle"&gt;Lorem Ipsum&lt;/h5&gt;' . '&lt;p class="item_block_right_details_orderingp"&gt;' . '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' . '&lt;/p&gt;' . '&lt;/div&gt;' . '&lt;/div&gt;';

下面的代码中肯定有个人喜好,但考虑以下几个关键点:

您将使用两种类型的 PHP 文件。用于视图(在其中呈现 HTML)和仅用于逻辑的视图。你写的更像是一种逻辑风格的方式,所有的东西都被包装在一个巨大的 PHP 标记中。

如果您正在渲染视图,我建议您围绕每一行逻辑打开和关闭 PHP。

这有助于以视图的形式更轻松地读取文件。我知道有很多开头和结尾,但是当你看到它时,我想你会同意它更容易解释。

最重要的一点 - 缩进 - 执行上述操作意味着您可以保持开始和结束标记正确缩进,并且可以更轻松地查看元素的打开和关闭位置。

以下是我编写此文件的方式。我没有测试过这个文件,因为我没有数据可以用它来渲染,但它向你展示了这个概念,而且缩进让我相信它是正确的。

<?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; ?>

接下来您应该做的是将这个文件分成多个文件以使其更易于管理。然后,您可以使用 PHP 来包含构成更大文件的不同位。

您可以使用include 来执行此操作。

希望这会有所帮助。

【讨论】:

  • 是的!这非常有帮助。这似乎也是 ACF 建议的方式。一种方法对页面性能比另一种更好吗?我开始以这种方式编写 php,因为我读到它对页面加载速度更好。不过,我绝对明白为什么你的建议更清楚了。
  • 很高兴 id 很有用 :) No it doesn't change performance.
猜你喜欢
  • 1970-01-01
  • 2020-08-24
  • 1970-01-01
  • 2018-07-05
  • 2018-09-11
  • 2020-03-19
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
相关资源
最近更新 更多