从acf中继器到cf7下拉列表的值数组



我正试图用acf中继器的值填充cf7下拉列表。如果我使用一个常规的硬编码数组,它工作得很好,所以在获取中继器字段的值时,不知何故我搞砸了。

以下是我得到的rn,试图将值推入数组:

add_filter('wpcf7_form_tag_data_option', function($n, $options, $args) {
if (in_array('gigs', $options)){
$gigs = array();
if( have_rows('termine') ):
while ( have_rows('termine') ) : the_row();
$gigs[] = get_sub_field('termin');
endwhile;
endif;
return $gigs;

}
return $n;
}, 10, 3);

尝试将return语句移动一点,但这也没有帮助,我对自己几乎不存在的php知识感到不知所措。

如果我有任何错误的想法或建议,我们将不胜感激。

您必须返回$n,它要么为null,要么为数组值。你很接近,但你的回报是错误的。如何使用这个过滤器的一个很好的例子是通过查看listo.php中的代码,您可以看到这个过滤器的正确用法。

话虽如此。。。如果不测试你的ACF值,我不能说你的函数是否会返回这些。。。但下面的函数经过测试,如果ACF函数有效,它将用CCD_ 2将数据返回给您的选择。

要检索包含post的post_id,您需要深入到unit标记中,并获取页面id。全局$post在这个过滤器中不起作用,因为它没有将任何循环属性传递给函数,所以您必须使用第二个参数指定ACF字段,该参数需要是父post id。

add_filter( 'wpcf7_form_tag_data_option', 'dd_filter_form_tag_data', 10, 3 );
function dd_filter_form_tag_data( $n, $options, $args ) {
// Get the current form.
$cf7 = wpcf7_get_current_contact_form();
// Get the form unit tag.
$unit_tag = $cf7->unit_tag();
// Turn the string into an array.
$tag_array = explode( '-', $unit_tag );
// The 3rd item in the array will be the page id.
$post_id = substr( $tag_array[2], 1 );
if ( in_array( 'gigs', $options, true ) ) {
$gigs = array();
if ( have_rows( 'termine', $post_id ) ) :
while ( have_rows( 'termine', $post_id ) ) :
the_row();
$gigs[] = get_sub_field( 'termin' );
endwhile;
endif;
$n = array_merge( (array) $n, $gigs );
}
return $n;
}

最新更新