php - Custom Field Select Box (Adding Select Keyword) -
i have added couple of custom fields wordpress posts, theme creating hobby (cars). having trouble selecting option html select box. if select it, works , save correctly ~ displaying correct value on front end, when return post page, shows first value of options, , not chosen one. example, if select automatic, show on front end, when revisit backend manual again.
i know stick selected post, have add selected keyword choice, having problems.
what i've done
what have done far work tutorial: http://wpshed.com/create-custom-meta-box-easy-way/
i created select box so:
function wpshed_meta_box_output( $post ) { // create nonce field wp_nonce_field( 'my_wpshed_meta_box_nonce', 'wpshed_meta_box_nonce' ); ?> <p> <label for="transmission_textfield"><?php _e( 'transmition', 'wpshed' ); ?>:</label> <!-- <input type="text" value="<?php echo wpshed_get_custom_field( 'transmission_textfield' ); ?>" /> --> <select name="transmission_textfield" id="transmission_textfield"> <option value="manual" >manual</option> <option value="automatic">automatic</option> </select> </p> } function wpshed_meta_box_save( $post_id ) { if( defined( 'doing_autosave' ) && doing_autosave ) return; if( isset( $_post['transmission_textfield'] ) ) update_post_meta( $post_id, 'transmission_textfield', esc_attr( $_post['transmission_textfield'] ) ); } add_action( 'save_post', 'wpshed_meta_box_save' ); ?>
how can add selected keyword option chosen?
you need set selected
property manually on <option>
- ie. value , use wordpress function selected()
output property in relevant place this:
<?php $selected_option = get_post_meta($post->id, 'transmission_textfield', true); ?> <select name="transmission_textfield" id="transmission_textfield"> <option value="manual" <?php selected($selected_option, 'manual') ?>>manual</option> <option value="automatic" <?php selected($selected_option, 'automatic') ?>>automatic</option> </select>
you don't post code wpshed_get_custom_field()
, won't use it, i'm guessing it's wrapper get_post_meta()
... in case can go ahead , use instead...
Comments
Post a Comment