获取存储在数据库中的转发器子字段的所有值



目前,我正在努力使用ACF(高级自定义字段(中继器字段。第一个问题是这种字段类型的一般架构 - 它没有存储在数据库中的单个位置,但它成倍增加(例如,如果我的中继器字段名称称为workshops_grouping子字段称为数据,那么将有workshops_grouping_1_data,workshops_grouping_2_data等(。

所以我想比 ACF(:) (更聪明,我想如果我使用以下代码从 CPT 获取所有帖子 ID,那么它会更容易。这是代码:

<?php
$other_page = get_posts( array(
'posts_per_page' => -1,
'fields' => 'ids',
'post_type' => 'workshops',
'post_status' => 'publish',
) );
if( have_rows('workshops_grouping', $other_page) ): ?>
<select id="no1">
<?php while( have_rows('workshops_grouping', $other_page) ): the_row(); ?>
<option value="<?php the_sub_field('data'); ?>"><?php the_sub_field('data'); ?></option>
<?php endwhile; ?>
</select>
<select id="no2">
<?php while( have_rows('workshops_grouping', $other_page) ): the_row(); ?>
<option value="<?php the_sub_field('company'); ?>"><?php the_sub_field('company'); ?></option>
<?php endwhile; ?>
</select>
<?php endif; ?>

但是,嘿,你猜怎么着 - 它根本不起作用。含义 = 它什么也没显示。我在ACF的官方社区委员会上也浏览了几篇类似的帖子。到目前为止没有成功。它甚至可行吗?

根据 ACF 文档,您必须在 have_rows 函数中传递一个 ID。 https://www.advancedcustomfields.com/resources/have_rows/

现在你在函数中传递 $other_page 变量。此变量是包含页面的数组。您需要先循环这个并在 de have_rows 函数中传递 post ID。

您的代码将如下所示:

<?php
$other_page = get_posts( array(
'posts_per_page' => -1,
'fields' => 'ids',
'post_type' => 'workshops',
'post_status' => 'publish',
) );
foreach ( $other_page as $page ) {
if( have_rows('workshops_grouping', $page->ID) ): ?>
<select id="no1">
<?php while( have_rows('workshops_grouping', $page->ID) ): the_row(); ?>
<option value="<?php the_sub_field('data'); ?>"><?php the_sub_field('data'); ?></option>
<?php endwhile; ?>
</select>
<select id="no2">
<?php while( have_rows('workshops_grouping', $page->ID) ): the_row(); ?>
<option value="<?php the_sub_field('company'); ?>"><?php the_sub_field('company'); ?></option>
<?php endwhile; ?>
</select>
<?php endif;
}

确保变量 $other_page 有一些页面,ACF 字段workshops_grouping有一些行。

最新更新