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 have created a meta box with a select input (drop down menu). It has a few values and I want to display different content depends on which value the user selects. I am just unsure how to display this conditional content with post meta select boxes.

<label for="item_one">Item One Select:</label>
<select name="item_one" id="item_one">
    <option value="option1" <?php selected( $item_one_selected, 'option1' ); ?>>Something 1</option>
    <option value="option2" <?php selected( $item_one_selected, 'option2' ); ?>>Something 2</option>
    <option value="option3" <?php selected( $item_one_selected, 'option3' ); ?>>Something 3</option>
</select>

Can I do something similar to a checkbox like:

<?php if (get_post_meta($post->ID, 'metavaluehere', true)) { ?>
//do something 
<? } else { ?>
//do something else
<? } else { ?>
//do something else
<?php } ?>

I am just unsure the proper way to form this conditional statement with a metabox select input.

In plain english I'd want this, using the top example:

If option1 is selected do this, else if option 2 is selected do this instead, else if option 3 is selected do this...

Any help would be greatly appreciated!

Full code here:

<?php
add_action( 'add_meta_boxes', 'post_options_box' );
function post_options_box()
{
    add_meta_box( 'post-options-box', 'Post Options', 'post_options_callback', 'post', 'normal', 'high' );
}


function post_options_callback( $post ){
    $options_values = get_post_custom( $post->ID );
    $item_one_selected = isset( $options_values['item_one'] ) ? esc_attr( $options_values['item_one'][0] ) : '';
    wp_nonce_field( 'my_post_options_meta_nonce', 'post_options_meta_nonce' );
 ?>
 <p>
 <label for="item_one">Item One Select:</label>
    <select name="item_one" id="item_one">
        <option value="option1" <?php selected( $item_one_selected, 'option1' ); ?>>Something 1</option>
        <option value="option2" <?php selected( $item_one_selected, 'option2' ); ?>>Something 2</option>
        <option value="option3" <?php selected( $item_one_selected, 'option3' ); ?>>Something 3</option>
    </select>
   </p>


   <?php    
}

add_action( 'save_post', 'post_options_save' );
function post_options_save ( $post_id )
{
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    if( !isset( $_POST['price_meta_nonce'] ) || !wp_verify_nonce( $_POST['price_meta_nonce'], 'my_price_meta_nonce' ) ) return;

    if( !current_user_can( 'edit_post' ) ) return;



    if( isset( $_POST['item_one'] ) )
        update_post_meta( $post_id, 'item_one', esc_attr( $_POST['item_one'] ) );


}
?> 
share|improve this question
    
How is the data saved to the database? –  s_ha_dum Jun 27 '13 at 21:44
    
It's being saved just didnt include that part of the code, didn't think it was necessary. –  Jacob Jun 27 '13 at 21:48
    
But the way it is saved determines what get_post_meta gives back and that determines how your conditional will have to work. –  s_ha_dum Jun 27 '13 at 21:51
    
Ok, I am going to add it to the original post, sorry about that. –  Jacob Jun 27 '13 at 22:01
1  
^^ In other words, are you saving a serialized array or several individual key/value pairs? –  s_ha_dum Jun 27 '13 at 22:01
show 1 more comment

1 Answer

up vote 1 down vote accepted

I can't test this but it looks like something like what you have should work:

if ('option1' == get_post_meta($post->ID, 'item_one', true)) { 
  //do something 
} elseif ('option2' == get_post_meta($post->ID, 'item_one', true)) {
  //do something else
} else { 
  //do something else
} 

You didn't need all those opening and closing tags, and some of them would have caused odd results.

share|improve this answer
    
thanks! this worked but I am using those opening and closing tags you mentioned. The reason is that I need to show/hide div elements according to the meta values; I am unaware of any other way to to do this other than using the tags I posted above; please let me know if there is a better way!! Thanks again!<3 –  Jacob Jun 27 '13 at 23:12
    
If you are using the opening and closing tags to switch between PHP and HTMP, then that is what they are for, but in the code the posted you were not doing that. –  s_ha_dum Jun 27 '13 at 23:24
    
Ahh ok, I probably wasn't specific enough in my OP. I thank you so much for helping me out. You made my day. –  Jacob Jun 27 '13 at 23:33
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.