我试图在single-services.php
页面和服务上显示自动选择值是我的自定义帖子类型。在single-services.php
页面上,我有一个选择框,显示了所有的服务岗位的标题,由联系表单7插件创建的选项。我想知道如何根据用户当前访问的服务岗位自动选择选项值。
这是我的函数,用于添加function .php中选择框中的所有服务帖子
function add_posttype_to_CF7 ( $tag, $unused ) {
if ( $tag['name'] != 'services' ){
//continue only for the specific field - here we check by the name of the field
return $tag;
}
//get a list of all active services
$services = get_posts(array(
'post_type' => 'service',
'post_status' => 'publish',
'posts_per_page' => -1
));
foreach ( $services as &$services ) {
$tag['raw_values'][] = $services->post_title;
$tag['values'][] = $services->post_title;
$tag['labels'][] = $services->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'add_posttype_to_CF7', 10, 2);
我用JQuery解决了这个问题。
<script>
jQuery(document).ready( function($) {
var h1Text = "<?php echo $service_title ?>";
jQuery('#service_id[name="services"]').find('option[value="'+h1Text+'"]').prop("selected",true);
});</script>