我正在尝试将转发器字段添加到灵活的内容行中,但由于某种原因没有输出任何内容。我检查了字段,看起来是正确的,所以有人可以指出我出错的地方吗?感谢
<?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; ?>
答案 0 :(得分:0)
您的<?php if(get_field('collection_images_grid')): ?>
声明应为<?php if(get_sub_field('collection_images_grid')): ?>
答案 1 :(得分:0)
<?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;
}
?>
答案 2 :(得分:0)
您应该在代码中更改一件事。将<?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]
答案 3 :(得分:0)
假设转发器字段为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/
答案 4 :(得分:0)
要调试页面上的所有“高级自定义字段”并更好地了解其结构,我经常使用以下PHP代码段:
echo '<pre>';
var_dump(get_fields());
echo '</pre>';
这有助于确保数据可用,并弄清楚如何在嵌套结构中访问数据。