【问题标题】:ACF Repeater Field in Flexible Content灵活内容中的 ACF 中继器字段
【发布时间】:2013-12-29 21:49:46
【问题描述】:
我正在尝试将转发器字段添加到灵活的内容行中,但由于某种原因没有输出任何内容。我检查了这些字段,它们看起来是正确的,所以有人可以指出我哪里出错了吗?谢谢
<?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>
<div>
<h4><?php the_sub_field("collection_title"); ?></h4>
</div>
<?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>
<?php if(get_field('collection_images_grid')): ?>
<?php while(has_sub_field('collection_images_grid')): ?>
<div class="collections">
<span><strong><?php the_sub_field('collection_detail'); ?></strong></span>
<a href="<?php the_sub_field('product_link'); ?>">
<img src="<?php the_sub_field('collection_image'); ?>"/>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
【问题讨论】:
标签:
wordpress
advanced-custom-fields
【解决方案1】:
您的<?php if(get_field('collection_images_grid')): ?> 语句应为<?php if(get_sub_field('collection_images_grid')): ?>
【解决方案2】:
<?php
// check if the flexible content field has rows of data
if( have_rows('the_process') ){
// loop through the rows of data
while ( have_rows('the_process') ) : the_row();
if( get_row_layout() == 'content' ){
?>
<h1><?php the_sub_field('headline');?></h1>
<h2 class="tagLine paddingBottom80"><?php the_sub_field('sub_headline');?></h2>
<div class="steps clearAfter">
<?php if(get_sub_field('process_steps')): ?>
<?php
while(has_sub_field('process_steps')):
?>
<!--Step-->
<div class="processStep rel boxSizing">
<img src="images/ph.png" width="200" height="200" class="borderRadius50Perc imageBorder boxSizing" />
<div class="processBox border1 padding20 clearAfter">
<div class="third processNumber boxSizing font70 darkBlue">
<div class="border1 padding20">
<?php echo $i;?>
</div>
</div>
<div class="twothird boxSizing processContent">
<h3><?php the_sub_field('step_headline'); ?></h3>
<div class="processContentTxt grey">
<?php the_sub_field('step_content'); ?>
</div>
</div>
</div>
</div>
<!--Step-->
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php
}
endwhile;
}
?>
【解决方案3】:
您应该更改代码中的一件事。将 <?php if(get_field('collection_images_grid')): ?> 更改为 <?php if(get_sub_field('collection_images_grid')): ?> 即可!我已经模拟了您的问题,更改为 sub_field 后它可以工作。您的代码将如下所示:
<?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>
<div>
<h4><?php the_sub_field("collection_title"); ?></h4>
</div>
<?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>
<?php if(get_sub_field('collection_images_grid')): ?>
<?php while(has_sub_field('collection_images_grid')): ?>
<div class="collections">
<span><strong><?php the_sub_field('collection_detail'); ?></strong></span>
<a href="<?php the_sub_field('product_link'); ?>">
<img src="<?php the_sub_field('collection_image'); ?>"/>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
我在 acf 灵活内容中使用中继器时遇到了一些问题。因此,如果我能在这种情况下说点帮助的话,那就是使用变量来打印中继器数组的元素,如下所示:
<?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>
<div>
<h4><?php the_sub_field("collection_title"); ?></h4>
</div>
<?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>
<?php if(get_sub_field('collection_images_grid')): ?> // considering that collections_images_grid are a repeater
<?php $collection_image = get_sub_field('collection_images_grid');?>
<?php echo $collection_image['url']; ?> <?php echo $collection_image['alt']; ?> //Or something that you would like to use [... and than the rest of code]
【解决方案4】:
假设您的转发器字段是collection_images_grid,您应该循环使用have_rows() 的项目,如下所示:
<?php if(get_row_layout() == "collection_title"): // layout: Collection Title ?>
<div>
<h4><?php the_sub_field("collection_title"); ?></h4>
</div>
<?php elseif(get_row_layout() == "collection_images"): // layout: Collection Images ?>
<?php if(have_rows('collection_images_grid')): ?>
<?php while(have_rows('collection_images_grid')): the_row(); ?>
<div class="collections">
<span><strong><?php the_sub_field('collection_detail'); ?></strong></span>
<a href="<?php the_sub_field('product_link'); ?>">
<img src="<?php the_sub_field('collection_image'); ?>"/>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endif; ?>
这实质上是检查灵活内容字段是否有数据行 (<?php if(have_rows('collection_images_grid')): ?>),然后循环/显示它们 (<?php while(have_rows('collection_images_grid')): the_row(); ?>)。
有关循环使用have_rows() 的字段的更多详细信息:https://www.advancedcustomfields.com/resources/have_rows/
【解决方案5】:
为了调试页面上的所有高级自定义字段并更好地了解结构,我经常使用以下PHP sn-p:
echo '<pre>';
var_dump(get_fields());
echo '</pre>';
这有助于确保数据可用,并弄清楚如何在嵌套结构中访问它。