Mat*_*ttM 5 wordpress contact-form-7 custom-post-type
我想在Wordpress中使用联系表单7来构建订单表单.我希望订单表格的内容填充自定义帖子类型"贸易展示材料"的内容 - 帖子类型包含字段"名称""数字""描述""照片".我们的想法是可以从表格中选择每件作品.任何人都可以为此提供总体方向吗?我应该完全使用另一个插件吗?
vic*_*nte 13
也许你可以使用wpcf7_form_tag过滤器钩子.
如果您想使用自定义帖子类型作为下拉列表(选择)的选项,您可以在functions.php中添加如下示例:
function dynamic_field_values ( $tag, $unused ) {
if ( $tag['name'] != 'your-field-name' )
return $tag;
$args = array (
'numberposts' => -1,
'post_type' => 'your-custom-post-type',
'orderby' => 'title',
'order' => 'ASC',
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$tag['raw_values'][] = $custom_post->post_title;
$tag['values'][] = $custom_post->post_title;
$tag['labels'][] = $custom_post->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2);
Run Code Online (Sandbox Code Playgroud)
在表单中,您可以添加字段:
[select* your-field-name include_blank]
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,post_title用于下拉列表的选项中.您可以在此处添加自己的字段(名称,编号,描述,照片).
我不认为 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;
}
Run Code Online (Sandbox Code Playgroud)
然后您可以像这样使用联系表单 7 中的标签。
[customlist your-field-name]
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助像我一样正在寻找方法的其他人。
您可以更改它以从自定义帖子类型获取所需的任何信息。
但它没有任何验证。
| 归档时间: |
|
| 查看次数: |
7009 次 |
| 最近记录: |