我找到了另一篇文章,其中包含了用分类法的术语动态填充CF7下拉列表的代码,我已经让它正常工作了,但我希望这些术语不仅都在分类法中,而且只与它所在的文章相关。
为了给它更多的上下文,这是一个职位公告上的申请表。自定义职位类型是工作列表。我有位置分类法。这些术语被分配给一个职位。我只希望选定的术语显示在此下拉列表中。
这是我的代码:
function dynamic_select_list( $tag ) {
// Only run on select lists
if( \'select\' !== $tag[\'type\'] && (\'select*\' !== $tag[\'type\']) ) {
return $tag;
} else if ( empty( $tag[\'options\'] ) ) {
return $tag;
}
$term_args = array();
// Loop thorugh options to look for our custom options
foreach( $tag[\'options\'] as $option ) {
$matches = explode( \':\', $option );
if( ! empty( $matches ) ) {
switch( $matches[0] ) {
case \'taxonomy\':
$term_args[\'taxonomy\'] = $matches[1];
break;
case \'parent\':
$term_args[\'parent\'] = intval( $matches[1] );
break;
}
}
}
// Ensure we have a term arguments to work with
if( empty( $term_args ) ) {
return $tag;
}
// Merge dynamic arguments with static arguments
$term_args = array_merge( $term_args, array(
\'hide_empty\' => false,
) );
$terms = get_terms( $term_args );
// Add terms to values
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
$tag[\'values\'][] = $term->name;
}
}
return $tag;
}
add_filter( \'wpcf7_form_tag\', \'dynamic_select_list\', 10 );