我正在建立一个网站,我的朋友可以为她的商店创建新的颜色模式,但我想将其链接到联系订单表单。
目前,她必须在我设置的自定义字段和联系表单中创建它们。
例如:她创建了红色供人们选择,但是她必须在联系表格7的选择标签中键入“红色”。“farve”=“color”(丹麦语);)
<div class="form-group">
<label>Color</label>
[select* menu-farve class:form-control "red" "blue"]
现在我调用了自定义帖子类型slug。 “color”,但是如何创建一个可以在表单中添加的数组?
以及如何将其添加到表单中?
我有一个数组来显示自定义帖子类型的颜色名称,如果这有帮助:
<?php
$args = array( 'post_type' => 'color' );
$posts = get_posts( $args );
foreach ( $posts as $post ) :
$image = get_field('color_image', $post->ID);
setup_postdata( $post );
if ( get_field( 'sold' ) ): ; else: ?>
<div class="col-md-3">
<img class="img-responsive" src=" <?php echo $image['url'] ?> ">
<h2 class="">
<?php the_title(); ?>
</h2>
</div>
<?php endif ?>
<?php endforeach;
wp_reset_postdata(); ?>
答案 0 :(得分:1)
有几种方法可以动态生成Contact Form 7选择。
选项1:PHP
WordPress StackExchange和Lee Willis上的博客都找到了一个很好的解决方案,其中来自StackExchange的内容如下:
/** Dynamic List for Contact Form 7 **/
/** Usage: [select name term:taxonomy_name] **/
function dynamic_select_list($tag, $unused){
$options = (array)$tag['options'];
foreach ($options as $option)
if (preg_match('%^term:([-0-9a-zA-Z_]+)$%', $option, $matches))
$term = $matches[1];
//check if post_type is set
if(!isset($term))
return $tag;
$taxonomy = get_terms($term, array('hide_empty' => 0));
if (!$taxonomy)
return $tag;
foreach ($taxonomy as $cat) {
$tag['raw_values'][] = $cat->name;
$tag['values'][] = $cat->name;
$tag['labels'][] = $cat->name;
}
$tag['raw_values'][] = 'Other';
$tag['values'][] = 'Other';
$tag['labels'][] = 'Other - Please Specify Below';
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10, 2);
这是针对分类法,但可以编辑以使用您提供的数组,如下所示
$options = (array) $tag[‘options’];
foreach ( $options as $option ) {
if ( preg_match( ‘%^posttype:([-0-9a-zA-Z_]+)$%’, $option, $matches ) )
{
$post_type = $matches[1];
}
}
//check if post_type is set
if(!isset($post_type))
return $tag;
$args= array(
'post_type' => $post_type
);
$colors = get_posts($args);
if ( ! $colors )
return $tag;
foreach ( $colors as $color ) {
$tag['raw_values'][] = $color->post_title;
$tag['values'][] = $color->post_title;
$tag['labels'][] = $color->post_title;
}
return $tag;
}
选项2:插件
Contact Form 7 Dynamic Text Extension可能具有您正在寻找的功能。
答案 1 :(得分:0)
我在其他地方找到了我的解决方案,但代码几乎相同
function dynamic_field_values ( $tag, $unused ) {
if ( $tag['name'] != 'colorfield' )
return $tag;
$args = array (
'numberposts' => -1,
'post_type' => 'color',
'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);