Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am faced with a particular issues dealing with metabox fields. I've added a select dropdown list to Pages post type and what I can not figure out is how to add additional metafields based on the value of the select box. I was thinking something like if (metaboxfield.value == 'value1') then display the code for the new metabox field?

Here is what I am trying to accomplish...I can't figure out how to pull the value of a select box.

<?php
    $page_meta_box = array(
        'id'          => 'page_meta_box',
        'title'       => 'Page Options',
        'desc'        => '',
        'pages'       => array( 'page' ),
        'context'     => 'normal',
        'priority'    => 'high',
        'fields'      => array(
            array(
                'id'          => 'page_select',
                'type'        => 'select',   
                'choices'     => array(
                    array(
                        'label'       => 'Value 1',
                        'value'       => 'value1'
                    ),
                    array(
                        'label'       => 'Value 2',
                        'value'       => 'value2'
                    ),

                    // If value 2 is selected, display an additional select box 
                )     
            )
        )
    );

?>

From the front end it would be a matter of

$getPageSelect = get_post_meta(get_the_ID(), 'page_select', true);

I've been stuck on this for a while and have yet to figure out the best way to approach this. If someone could point me in the right direction, that would be much appreciated.

Thanks!

share|improve this question
    
Do you just want a javascript solution? Or do you need an example of how to check the value of $getPageSelect to determine whether or not to output the 2nd select box after hitting submit? –  sanchothefat Feb 13 at 17:20
    
Essentially this is what should happen. If value 1 is selected store that value in $getPageSelect, if Value 2 is selected, display an additional select box and store the value of that new select box in $getPageSelect. So, onChange of the first select, get the value. If value = 2 then display additional selectbox and get that value and store into $getPageSelect. –  user47569 Feb 13 at 17:23
    
But then once $getPageSelect is one of the values in the 2nd select box how will you know which value to show in the first select box so that the 2nd is still visible? It might help if you can show your code where you output the metabox and also where you save the metabox data. –  sanchothefat Feb 16 at 18:38

1 Answer 1

Create your metaboxes as you normally would, they will be visible by default. Then use js to hide the ones you dont want initially visible. Your code would look something like below. The example is for a checkbox, but you will get the idea how it works.

if ( $('input#_cmb_metaboxid_1').is(':checked') ) {
        $('#metabox_id_2').show();
    }
    else {
        $('#metabox_id_2').hide();
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.