drhanjuw56233 2015-07-08 20:37
浏览 54
已采纳

自定义字段选择框(添加选择关键字)

I have added a couple of custom fields to my WordPress posts, for a theme I am creating for my hobby (cars). I am having trouble with selecting an Option from an HTML select box. If I select it, it works and save correctly ~ displaying the correct value on the front end, but when I return to the post page, it always shows the first value of the options, and not the chosen one. For example, if I select Automatic, it will show on the front end, but when I revisit the backend it is Manual again.

I know that to get it to stick to the selected post, I have to add the selected keyword to the choice, but this is where I am having problems.

What I've done

What I have done so far is work from this tutorial: http://wpshed.com/create-custom-meta-box-easy-way/

I created a select box like so:

function wpshed_meta_box_output( $post ) {
  // create a 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 I add the selected keyword to the option chosen?

  • 写回答

1条回答 默认 最新

  • doubu0897 2015-07-08 20:49
    关注

    You need to set the selected property manually on the <option> - ie. get the value and then use the wordpress function selected() to output the property in the relevant place like 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 the code for wpshed_get_custom_field(), so I won't use it, but I'm guessing it's just a wrapper for get_post_meta()... in which case you can go ahead and use that instead...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行