我在将数据保存到选择框中时遇到麻烦。其他一切都可以节省。我可以选择该选项并保存帖子,但是在刷新时,选择字段再次重置为默认值,而其他字段保持设置。建议?
这是我的代码:
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 );
我以为这与向选项中添加选定函数有关,但是仍然没有任何作用。我也试过几次重写保存功能,但结果相同。
此元框的目的是设置样式和过滤帖子类型,以代替使用简码。
感谢您的帮助。谢谢。
您需要将所选选项设置为选中状态。与在复选框上使用checked()
的方式类似,您可以在select元素的选项元素上使用selected()
。