Home Forums Front-end Issues Show select value of dynamically populated menu


Show select value of dynamically populated menu

  • Hi I have a dynamcially populated menu using code similar to this:

    Here is my code:

    function acf_load_assign_company_field_choices( $field ) {
        // reset choices
        $field['choices'] = array();
        // if has rows
        if( have_rows('company_details', 'option') ) {
            // while has rows
            while( have_rows('company_details', 'option') ) {
                // instantiate row
                // vars
                $company_name = get_sub_field('company_name');
                $company_logo = get_sub_field('company_logo');
    			$company_number = get_sub_field('company_number');
                $company_email = get_sub_field('company_email');
                // append to choices
                $field['choices'][ $company_name ] = $company_name;
        // return the field
        return $field;

    I am then showing the dropdown menu in a users profile with my other ACF fields, I want to show the current ‘company’ the current user is assigned too on the front-end though and I have tried this but it is showing not the company the user has been asigned too as its getting it from the options page:

    <?php the_field('company_name','option'); ?>.

    Can anyone help me with this?

  • I have managed to create a text field in ACF called assigned_company which I have gave a css class too of assignedCompany and then in jquery told it to populate the text field with the value of whatever was chosen in the dynamically generated dropdown menu:

    $( ".assign_company select option" ).click(function() {
    		var text = $( this ).text();
    		$( ".assignedCompany input" ).val( text );

    and then have pulled that from the front-end however I can’t seem to get the other values: company_email, company_number, and company_logo. I would like to get the appropiate unique value depending on what company the user selected.

  • The main problem you have is that you’re using an options page for your companies and there’s really no way to reliably get the company name without once again looping through your repeater and checking each row of the repeater to see if the company matches.

    But then you have to think about what happens if you ever have to change the company name. This will cause the value that the user chose for company to not match anything.

    A better plan would be to use a custom post type ( for the companies and then you can use a post object field for the user to select a company. This would be much easier in the long run to manage and to code for.

  • Hi John,
    Thanks for replying its a difficult one, I recently changed it to custom post types and then like you said changed the repeater to be a post object, but even now im still struggling on how to do the appropiate SQL query because the current logged in user is assigned the company (which is a post type now) and a agent (another post type) but i need to display the companies details: logo, email address, telephone number and also the agents details (skype, email address, phone number, linkedin and a personal message) which should be personal to that user.

    I have looked at the database and it appears to be a difficult SQL query, I have no idea where to start.

  • I think that you want to have the user select field on the company page but it may be easier to do if the the company and agent are selected on the user edit page.

    So on the CPT for user there would be 3 relationship fields, one for the User, one for Company and one of Agent.

    You can get the current user and then do a query on the user cpt to find the page associated with that user.

    Hope that makes sense and helps.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Show select value of dynamically populated menu’ is closed to new replies.