Skip to content Skip to sidebar Skip to footer

How To Get A Dynamic Dropdown With An Acf Relationship Field

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 c

Solution 1:

To make things easier you could add the email address as a data attribute to the tag for each provider title like so:

<selectname="supplier"id="supplier"class="form-control"><optionvalue="">---</option><?php$posts = get_field('course_providers');
                    if( $posts ): ?><?phpforeach( $postsas$post): ?><?php setup_postdata($post); ?><optionvalue="<?php the_title(); ?>"data-email="<?php the_field('email_address'); ?>"><?php the_title(); ?></option><?phpendforeach; ?><?php wp_reset_postdata(); ?><?phpendif; ?></select>

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
});

Post a Comment for "How To Get A Dynamic Dropdown With An Acf Relationship Field"