循环显示来自 ACF 组的所有 fields/rows
Loop to display all fields/rows from ACF group
我想使用以下代码显示 ACF 组之一的所有行。不幸的是什么都没有出现。我可以寻求帮助吗?
<div class="product-atributes">
<div class="atributes-form">
<?php
for ($i=1; $i<3; $i++) {
if (have_rows($atrybut.$i)) {
while (have_rows('atrybut_'.$i)) {
the_row();
// vars
$quantity = get_sub_field('quantity');
$ean = get_sub_field('ean');
$linkap = get_sub_field('linkap');
?>
<div class="flex-body">
<div class="flex-column">
<div>Quantity: <?php echo $quantity ?></div>
<div>EAN: <?php echo $ean ?></div>
</div>
<div class="flex-row">
<div><span><a href="<?php echo $linkap ?>" target="_blank" rel="nofollow">BUY</a></span></div>
</div>
</div>
<?php } } }
?>
</div>
</div>
看来你这里有错字:
if (have_rows($atrybut.$i)) {
while (have_rows('atrybut_'.$i)) {
哪个是正确的字段名称 $atrybut.$i
或 'atrybut_'.$i
?
您还缺少一些结尾 ;
...
更新:添加条件显示:
...
$quantity = get_sub_field('quantity');
$ean = get_sub_field('ean');
$linkap = get_sub_field('linkap');
// may need to tweak this logic based on what the
// values might/could be but this should get you going
$has_all_acf_fields = $quantity && $ean && $linkap;
if ($has_all_acf_fields) {
?>
<div class="flex-body">
<div class="flex-column">
<div>Quantity: <?php echo $quantity; ?></div>
<div>EAN: <?php echo $ean; ?></div>
</div>
<div class="flex-row">
<div><span><a href="<?php echo $linkap; ?>" target="_blank" rel="nofollow">BUY</a></span></div>
</div>
</div>
<?php
}
...
如果要获取 ACF Group
字段,则必须先获取 group_field_name
。喜欢下面的。
<?php
$main_group_field = get_field('GROUP-FIELD-NAME');
// Group's field returs array. So can fetch subfields like this.
$quantity = $main_group_field['quantity'];
$ean = $main_group_field['ean'];
$linkap = $main_group_field['linkap'];
?>
您可以在此处查看更多详细信息:https://www.advancedcustomfields.com/resources/group/
我想使用以下代码显示 ACF 组之一的所有行。不幸的是什么都没有出现。我可以寻求帮助吗?
<div class="product-atributes">
<div class="atributes-form">
<?php
for ($i=1; $i<3; $i++) {
if (have_rows($atrybut.$i)) {
while (have_rows('atrybut_'.$i)) {
the_row();
// vars
$quantity = get_sub_field('quantity');
$ean = get_sub_field('ean');
$linkap = get_sub_field('linkap');
?>
<div class="flex-body">
<div class="flex-column">
<div>Quantity: <?php echo $quantity ?></div>
<div>EAN: <?php echo $ean ?></div>
</div>
<div class="flex-row">
<div><span><a href="<?php echo $linkap ?>" target="_blank" rel="nofollow">BUY</a></span></div>
</div>
</div>
<?php } } }
?>
</div>
</div>
看来你这里有错字:
if (have_rows($atrybut.$i)) {
while (have_rows('atrybut_'.$i)) {
哪个是正确的字段名称 $atrybut.$i
或 'atrybut_'.$i
?
您还缺少一些结尾 ;
...
更新:添加条件显示:
...
$quantity = get_sub_field('quantity');
$ean = get_sub_field('ean');
$linkap = get_sub_field('linkap');
// may need to tweak this logic based on what the
// values might/could be but this should get you going
$has_all_acf_fields = $quantity && $ean && $linkap;
if ($has_all_acf_fields) {
?>
<div class="flex-body">
<div class="flex-column">
<div>Quantity: <?php echo $quantity; ?></div>
<div>EAN: <?php echo $ean; ?></div>
</div>
<div class="flex-row">
<div><span><a href="<?php echo $linkap; ?>" target="_blank" rel="nofollow">BUY</a></span></div>
</div>
</div>
<?php
}
...
如果要获取 ACF Group
字段,则必须先获取 group_field_name
。喜欢下面的。
<?php
$main_group_field = get_field('GROUP-FIELD-NAME');
// Group's field returs array. So can fetch subfields like this.
$quantity = $main_group_field['quantity'];
$ean = $main_group_field['ean'];
$linkap = $main_group_field['linkap'];
?>
您可以在此处查看更多详细信息:https://www.advancedcustomfields.com/resources/group/