使用 PHP 从 ACF 中提取嵌套的转发器内容——关闭标签的问题
Pulling nested repeater content from ACF with PHP — issue with closing tags
我是 php 的新手,我很难确定结束标记(主要是 div)需要放在这个 PHP 中的什么位置以匹配原始 HTML 我编码时没有动态内容(对于 Wordpress 网站)。现在我的页脚(下面未显示)被推来推去,因为 many/not 太多了,元素没有正确嵌套。
代码的功能是从高级自定义字段中的嵌套转发器中提取信息,循环遍历每个元素并将其放入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>
谢谢大家!
在一行中作为回声打开和关闭元素会给您带来各种各样的问题。这条线是我要避免的主要线(这是我认为错误的地方。
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>';
下面的代码肯定有个人喜好,但有几个要点考虑如下:
您将使用两种类型的 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 来执行此操作。
希望对您有所帮助。
我是 php 的新手,我很难确定结束标记(主要是 div)需要放在这个 PHP 中的什么位置以匹配原始 HTML 我编码时没有动态内容(对于 Wordpress 网站)。现在我的页脚(下面未显示)被推来推去,因为 many/not 太多了,元素没有正确嵌套。
代码的功能是从高级自定义字段中的嵌套转发器中提取信息,循环遍历每个元素并将其放入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>
谢谢大家!
在一行中作为回声打开和关闭元素会给您带来各种各样的问题。这条线是我要避免的主要线(这是我认为错误的地方。
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>';
下面的代码肯定有个人喜好,但有几个要点考虑如下:
您将使用两种类型的 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 来执行此操作。
希望对您有所帮助。