如何使用"ACF关系"字段获取动态下拉列表 [英] How to get a dynamic dropdown with an ACF Relationship field
问题描述
我的网站列出了课程和课程提供者.我正在使用ACF关系来将提供程序分配给每门课程.课程和课程提供者都是自定义帖子类型.我为每个课程页面都有一个联系表,我需要从下拉列表中将查询发送给选定的课程提供者.我正在尝试做的是创建一个动态字段,在该字段中,一旦选择了提供者,就将他们的电子邮件(分配为acf字段)提取到另一个表单字段中,并将表单提交给该特定电子邮件.我在以下代码中同时获得了分配的提供者列表和他们的电子邮件.
My website lists courses and course providers. I'm using ACF relationship to assign the providers to each course. both courses and course providers are custom post types I have a contact form for each single course page and I need to send the enquiry to the selected course provider from a dropdown. What I'm trying to do is to have a dynamic field where, once a provider has been selected, fetch their email (assigned as a acf field) into another form field and submit the form to that specific email. I'm getting both the list of assigned providers and their emails in following code.
<select name="supplier" id="supplier" class="form-control">
<option value="">---</option>
<?php
$posts = get_field('course_providers');
if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<option value="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</select>
<select name="supplier_email" id="supplier_email" class="form-control">
<option value="">---</option>
<?php
$posts = get_field('course_providers');
if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<option value="<?php the_field('email_address'); ?>"><?php the_field('email_address'); ?></option>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</select>
我希望这可以使用jQuery完成.
I hope this can be done with jQuery.
谢谢!
推荐答案
为使事情变得简单,您可以将电子邮件地址作为数据属性添加到每个提供商标题的标签中,如下所示:
To make things easier you could add the email address as a data attribute to the tag for each provider title like so:
<select name="supplier" id="supplier" class="form-control">
<option value="">---</option>
<?php
$posts = get_field('course_providers');
if( $posts ): ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<option value="<?php the_title(); ?>" data-email="<?php the_field('email_address'); ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</select>
然后在jQuery中,您可以执行以下操作:
Then in your jQuery you can do something like:
$(document).ready(function() {
$('select#supplier').change(function() {
var emailAddress = $('select#supplier').find(':selected').data('email');
});
// Do something with the var emailAddress on form submit here
});
这篇关于如何使用"ACF关系"字段获取动态下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!