我正在使用contact for 7创建表单。目前,我正在从自定义帖子类型标题中自动填写文本名称字段下的值,请参见以下内容:
$post_ids = new WP_Query(array(
\'post_type\' => \'Buyers\', // replace with CPT name
\'fields\' => \'fname\' // replace with custom field name
));
$name = array();
// go through each of the retrieved ids and get the title
if ($post_ids->have_posts()):
foreach( $post_ids->posts as $id):
// get the post title, and apply any filters which plugins may have added
// (get_the_title returns unfiltered value)
$name[] = apply_filters(\'the_title\', get_the_title($id));
endforeach;
endif;
<script>
$( "#autocomplete" ).autocomplete({
source: <?php echo json_encode($name); ?>
});
</script>
现在,我想根据上面选择的帖子标题,用帖子中的自定义字段值自动填充其他表单字段(如姓氏、地址、联系人信息等)。我们如何才能做到这一点?
我想要以下内容:
<label> Your Name </label>
<input type="text" id="name" name="name" />
<input readonly="readonly" type="text" id="iprice" name="iprice" size="5">
<input readonly="readonly" type="text" id="icode" name="icode" size="3">
[submit "Send"]
$(function() {
$(\'#iprice\').val("");
$(\'#icode\').val("");
$("#name").autocomplete({
source: [{"label":"Air Soft Gun","price":"212","abbrev":"BMW"},
{"label":"Pepsi Cola Hat","price":"24","abbrev":"CRY"},
{"label":"Candle Lights Dinner","price":"780","abbrev":"NSS"},
{"label":"Pork Meat Ball","price":"178","abbrev":"SZK"},
{"label":"Granny Health Supplement","price":"24","abbrev":"TYT"}],
minLength: 2,
select: function(event, ui) {
$(\'#iprice\').val(ui.item.price);
$(\'#icode\').val(ui.item.abbrev);
}
});
$["ui"]["autocomplete"].prototype["_renderItem"] = function( ul, item) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( $( "<a></a>" ).html( item.label ) )
.appendTo( ul );
};
});
标签将是我的文章标题,并基于所选的文章倾斜,在价格和icode下显示自定义字段值。
任何帮助都将不胜感激。提前谢谢你。
最合适的回答,由SO网友:Swati 整理而成
我想出来了。以下是解决方案:
表单代码:
<label> Your Name </label>
<input type="text" id="name" name="name" />
<input readonly="readonly" type="text" id="iprice" name="iprice" size="5">
PHP代码
<?php
$post_id = array(
\'post_type\' => \'your_post_type_name\'
);
$post_data = array();
$my_query = new WP_Query( $post_id );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
$post_data[] = array(
\'label\' => get_the_title($post_id->ID),
\'price\' => get_post_meta( get_the_ID(), \'custom_field name\', true )
);
}
}
?>
我的脚本代码:
<script type="text/javascript">
$(function() {
$(\'#iprice\').val("");
$("#name").autocomplete({
source: <?php echo json_encode( $post_data); ?>,
minLength: 2,
select: function(event, ui) {
$(\'#iprice\').val(ui.item.price);
}
});
$["ui"]["autocomplete"].prototype["_renderItem"] = function( ul, item) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( $( "<a></a>" ).html( item.label ) )
.appendTo( ul );
};
});
</script>