我认为 wpcf7_form_tag 的工作方式与 vicente 在他之前的出色回答中所展示的方式不同。自 2015 年以来,情况可能发生了变化。
如果您阅读此处,它将解释您需要如何使用 wpcf7_form_tag:https://contactform7.com/2015/01/10/adding-a-custom-form-tag/
考虑到这一点以及联系表格 7 中的其他帖子:https://contactform7.com/2015/02/27/using-values-from-a-form-tag/#more-13351
我想出了这段代码来为我拥有的自定义帖子类型创建一个自定义下拉列表。
add_action('wpcf7_init', 'custom_add_form_tag_customlist');
function custom_add_form_tag_customlist() {
wpcf7_add_form_tag( array( 'customlist', 'customlist*' ),
'custom_customlist_form_tag_handler', true );
}
function custom_customlist_form_tag_handler( $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( empty( $tag->name ) ) {
return '';
}
$customlist = '';
$query = new WP_Query(array(
'post_type' => 'CUSTOM POST TYPE HERE',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
));
while ($query->have_posts()) {
$query->the_post();
$post_title = get_the_title();
$customlist .= sprintf( '<option value="%1$s">%2$s</option>',
esc_html( $post_title ), esc_html( $post_title ) );
}
wp_reset_query();
$customlist = sprintf(
'<select name="%1$s" id="%2$s">%3$s</select>', $tag->name,
$tag->name . '-options',
$customlist );
return $customlist;
}
然后你像这样在联系表单 7 中使用标签。
[customlist your-field-name]
希望这可以帮助像我一样正在寻找方法的其他人。
您可以更改它以从自定义帖子类型中获取您需要的任何信息。
但它没有任何验证。