你如何在 wordpress 的 select 框中保存元数据?

How do you save meta in select box in wordpress?

我在 select 框中保存数据时遇到问题。其他一切都保存得很好。我可以 select 选项并保存 post,但在刷新时 select 字段再次重置为默认值,而其他字段保持设置。建议?

这是我的代码:

function category_list($tag, $taxonomy){
    $tax_terms = get_terms($taxonomy);

    foreach($tax_terms as $term_single) {
        print('<'.$tag.' value="'.$term_single->name.'">'.$term_single->name.'</'.$tag.'>');
    }
}

function page_options_metabox_defaults() {
    return array(
        'city' => '',
        'office' => ''
        'location_full' => '',
        'location_2' => '',
        'location_4' = ''
    );
}

function page_options_metabox() {
    // Can only be used on a single post type (ie. page or post or a custom post type).
    // Must be repeated for each post type you want the metabox to appear on.
    add_meta_box(
        'page_options_metabox', // Metabox ID
        'Page Options', // Title to display
        'page_options_render_metabox', // Function to call that contains the metabox content
        'page', // Post type to display metabox on
        'normal', // Where to put it (normal = main colum, side = sidebar, etc.)
        'default' // Priority relative to other metaboxes
    );
}
add_action( 'add_meta_boxes', 'page_options_metabox' );


// This is the function  makes a meta box to specify dialog tech numbers`
function page_options_render_metabox() {

    // Variables
    global $post; // Get the current post data
    $saved = get_post_meta( $post->ID, 'page_options', true ); // Get the saved values
    $defaults = page_options_metabox_defaults(); // Get the default values
    $details = wp_parse_args( $saved, $defaults ); // Merge the two in case any fields don't exist in the saved data
    ?>
        <fieldset>
            <!--the fields-->
            <h4>Locations</h4>
            <div>
            <!--city-->
            <label for="page_options_custom_metabox">
                    <?php _e( 'City', 'page_options' );?>
                </label>
                <select style="width:25%;"
                    type="text"
                    name="page_options_custom_metabox[city]"
                    id="page_options_custom_metabox_city">

                    <?php
                    category_list('option','location-city');
                    ?>

                    </select>
                    <br>

            <!--office-->
            <label for="page_options_custom_metabox">
                    <?php _e( 'Specific Office', 'page_options' );?>
                </label>
                <input
                    style="width:25%;"
                    type="text"
                    name="page_options_custom_metabox[office]"
                    id="page_options_custom_metabox_office"
                    value="<?php echo esc_attr( $details['office'] ); ?>"><br>
                <br>
                <br>

            <!--column count-->
            <strong><label  for="page_options_custom_metabox">
                    <?php _e( 'adjust columns', 'page_options' );?>
                </label></strong>
                <br>
                <!--full width-->
                <label for="page_options_custom_metabox">
                    <?php _e( 'Full Width', 'page_options' );?>
                </label>
                <input onclick="cbclick(event)"
                class="colcheck"
                    type="checkbox"
                    data="12"
                    id="page_options_custom_metabox_location_full"
                    name="page_options_custom_metabox[location_full]"
                    value="12"
                    <?php
                        checked( $details['location_full'], '12' );
                    ?>>
                <!--2 col-->
                <label for="page_options_custom_metabox">
                    <?php _e( '2 columns', 'page_options' );?>
                </label>
                <input onclick="cbclick(event)"
                class="colcheck"
                    type="checkbox"
                    data="6"
                    id="page_options_custom_metabox_location_2"
                    name="page_options_custom_metabox[location_2]"
                    value="6"
                    <?php
                        checked( $details['location_2'], '6' );
                    ?>>

                <!--3 col-->
                <label for="page_options_custom_metabox">
                    <?php _e( '3 columns', 'page_options' );?>
                </label>
                <input onclick="cbclick(event)"
                class="colcheck"
                    type="checkbox"
                    data="4"
                    id="page_options_custom_metabox_location_4"
                    name="page_options_custom_metabox[location_4]"
                    value="3"
                    <?php
                        checked( $details['location_4'], '3' );
                    ?>>
            <br>

            </div>
        </fieldset>
    <?php
    // Security field
    // This validates that submission came from the
    // actual dashboard and not the front end or
    // a remote server.
    wp_nonce_field( 'page_options_form_metabox_nonce', 'page_options_form_metabox_process' );
}


// Save the metabox
function page_options_save_metabox( $post_id, $post ) {
    // Verify that our security field exists. If not, bail.
    if ( !isset( $_POST['page_options_form_metabox_process'] ) ) return;
    // Verify data came from edit/dashboard screen
    if ( !wp_verify_nonce( $_POST['page_options_form_metabox_process'], 'page_options_form_metabox_nonce' ) ) {
        return $post->ID;
    }
    // Verify user has permission to edit post
    if ( !current_user_can( 'edit_post', $post->ID )) {
        return $post->ID;
    }
    // Check that our custom fields are being passed along
    // This is the `name` value array. We can grab all
    // of the fields and their values at once.
    if ( !isset( $_POST['page_options_custom_metabox'] ) ) {
        return $post->ID;
    }

    // Sanitize all data
    // Set up an empty array
    $sanitized = array();
    // Loop through each of our fields
    foreach ( $_POST['page_options_custom_metabox'] as $key => $detail ) {
        // Sanitize the data and push it to our new array
        // `wp_filter_post_kses` strips our dangerous server values
        // and allows through anything you can include a post.
        $sanitized[$key] = wp_filter_post_kses( $detail );
    }
    update_post_meta($post->ID, 'page_options', $sanitized);
}
add_action( 'save_post', 'page_options_save_metabox', 1, 2 );

我认为这与向选项添加 selected 函数有关,但仍然没有任何作用。我也试过几次重写保存功能,但结果相同。

此元框的目的是设计和过滤 post 类型,作为使用短代码的替代方法。

感谢任何帮助。谢谢。

您需要将 selected 选项设置为 selected。类似于您在复选框上使用 checked() 的方式,您可以在 select 元素的选项元素上使用 selected()

您可以按如下方式修改您的 category_list 函数:

function category_list( $tag, $taxonomy, $selected ){
    $tax_terms = get_terms($taxonomy);

    foreach ( $tax_terms as $term_single ) {
            print( '<'.$tag.' value="'.$term_single->name.'" ' . selected( $term_single->name, $selected ) . '>'.$term_single->name.'</'.$tag.'>');        
    } 
}

然后当您调用该函数时,将保存的值传递给 $selected 参数。

<?php category_list( 'option', 'location-city', $details[city] ); ?>