保存元框数据问题
Posted
技术标签:
【中文标题】保存元框数据问题【英文标题】:Save Meta Box Data Issue 【发布时间】:2013-12-10 14:51:08 【问题描述】:我的 wordpress 保存元框数据时遇到问题,我使用了一些教程但它们对我没有帮助,我搜索了很多但没有用
还告诉我元框数据保存在哪里,我的意思是在哪个表中
我给你看我的代码
函数.php
这是添加元框::
<?php
function downlaod_meta()
add_meta_box('download_id','Download Meta Box','ct_downlaod_meta','post','normal','high');
add_action('add_meta_boxes','downlaod_meta');
function ct_downlaod_meta($post)
$value = get_post_custom($post->ID);
$url = isset( $value['txt_meta_url'] ) ? esc_attr( $value['txt_meta_url'][0] ) : '';
$title = isset( $value['txt_meta_title'] ) ? esc_attr( $value['txt_meta_title'][0] ) : '';
$size = isset( $value['txt_meta_size'] ) ? esc_attr( $value['txt_meta_size'][0] ) : '';
$author = isset( $value['opt_meta_author'] ) ? esc_attr( $value['opt_meta_author'][0] ) : '';
$editor = isset( $value['txt_meta_editor'] ) ? esc_attr( $value['txt_meta_editor'][0] ) : '';
// Nonce to verify intention later
wp_nonce_field( 'save_download_meta', 'download_nonce' );
?>
<table border="0" cellspacing="6" cellpadding="0">
<tr>
<td>
<label><strong>Download URL</strong></label>
</td>
<td>
<input type="text" name="txt_meta_url" id="txt_meta_url" size="80" value="<?php echo $url ?>" />
</td>
</tr>
<tr>
<td>
<label><strong>Download Title</strong></label>
</td>
<td>
<input type="text" name="txt_meta_title" id="txt_meta_title" size="80" value="<?php echo $title ?>" />
</td>
</tr>
<tr>
<td>
<label><strong>Download Size</strong></label>
</td>
<td>
<input type="text" name="txt_meta_size" id="txt_meta_size" size="80" value="<?php echo $size ?>" />
</td>
</tr>
<tr>
<td>
<label><strong>Author</strong></label>
</td>
<td>
<select multiple="multiple" name="opt_meta_author" id="opt_meta_author">
<option value="Amjad" <?php selected( $author, 'Amjad' ); ?>>Amjad</option>
</select>
<br />
<span style="font-size:12px; font-style:italic; color:#b1b1b1;">Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</span>
</td>
</tr>
<tr>
<td>
<label><strong>Editor</strong></label>
</td>
<td>
<select name="opt_meta_editor" id="opt_meta_editor" style="width:70%;">
<option>:.. Select Editor ..:</option>
<?php
$args = array(
'orderby' => 'display_name',
'order' => 'ASC'
);
$user_query = new WP_User_Query($args);
foreach ( $user_query->results as $user )
?>
<option value="<?php echo $user->display_name ?>" <?php selected( $editor, $user->display_name ); ?>>
<?php echo $user->display_name ?>
</option>
<?php
?>
</select>
</td>
</tr>
</table>
<?php
?>
这是元框数据保存的地方
<?php
add_action('save_post','save_download_meta_data');
function save_download_meta_data($post_id)
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
if ( !wp_verify_nonce( $_POST['download_nonce'], basename( __FILE__ ) ) )
return $post_id;
if ( !isset( $_POST['txt_meta_url'] ))
update_post_meta( $post_id, 'txt_meta_url', esc_attr( $_POST['txt_meta_url'] ) );
if ( !isset( $_POST['txt_meta_title'] ))
update_post_meta( $post_id, 'txt_meta_title', esc_attr( $_POST['txt_meta_title'] ) );
if ( !isset( $_POST['txt_meta_size'] ))
update_post_meta( $post_id, 'txt_meta_size', esc_attr( $_POST['txt_meta_size'] ) );
if ( !isset( $_POST['opt_meta_author'] ))
update_post_meta( $post_id, 'opt_meta_author', esc_attr( $_POST['opt_meta_author'] ) );
if ( !isset( $_POST['opt_meta_editor'] ))
update_post_meta( $post_id, 'opt_meta_editor', esc_attr( $_POST['opt_meta_editor'] ) );
?>
请任何人帮助我
【问题讨论】:
【参考方案1】:我有办法
functions.php
加肉盒
<?php
function downlaod_meta($post)
add_meta_box('download_id','Download Details','ct_downlaod_meta','post','normal','high');
add_action('add_meta_boxes','downlaod_meta');
function ct_downlaod_meta($post)
$txt_meta_url = get_post_meta($post->ID, 'txt_meta_url', true);
$txt_meta_title = get_post_meta($post->ID, 'txt_meta_title', true);
$txt_meta_size = get_post_meta($post->ID, 'txt_meta_size', true);
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
$opt_meta_editor = get_post_meta($post->ID, 'opt_meta_editor', true);
?>
<table border="0" cellspacing="6" cellpadding="0">
<tr>
<td>
<label><strong>Download URL</strong></label>
</td>
<td>
<input type="text" name="txt_meta_url" id="txt_meta_url" size="80" value="<?php echo $txt_meta_url; ?>" />
</td>
</tr>
<tr>
<td>
<label><strong>Download Title</strong></label>
</td>
<td>
<input type="text" name="txt_meta_title" id="txt_meta_title" size="80" value="<?php echo $txt_meta_title ?>" />
</td>
</tr>
<tr>
<td>
<label><strong>Download Size</strong></label>
</td>
<td>
<input type="text" name="txt_meta_size" id="txt_meta_size" size="80" value="<?php echo $txt_meta_size ?>" />
</td>
</tr>
<tr>
<td>
<label><strong>Author</strong></label>
</td>
<td>
<select multiple="multiple" name="opt_meta_author" id="opt_meta_author">
<?php
$authors = new WP_Query(array( 'post_type' => 'author', 'orderby' => 'title', 'order' => 'ASC' ));
while($authors->have_posts()) :
$authors->the_post();
?>
<option value="<?php echo the_ID() ?>" <?php selected( $opt_meta_author, the_ID() ); ?>>
<?php echo the_title(); ?>
</option>
<?php
endwhile;
?>
</select>
<br />
<span style="font-size:12px; font-style:italic; color:#b1b1b1;">Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</span>
</td>
</tr>
<tr>
<td>
<label><strong>Editor</strong></label>
</td>
<td>
<select name="opt_meta_editor" id="opt_meta_editor" style="width:70%;">
<option>Select Editor</option>
<?php
$args = array(
'orderby' => 'display_name',
'order' => 'ASC'
);
$user_query = new WP_User_Query($args);
foreach ( $user_query->results as $user )
?>
<option value="<?php echo $user->ID ?>" <?php selected( $opt_meta_editor, $user->ID ); ?>>
<?php echo $user->display_name ?>
</option>
<?php
?>
</select>
</td>
</tr>
</table>
<?php
?>
保存元框数据
<?php
add_action('save_post','save_download_meta_data');
function save_download_meta_data()
global $post;
$txt_meta_url = $_POST['txt_meta_url'];
$txt_meta_title = $_POST['txt_meta_title'];
$txt_meta_size = $_POST['txt_meta_size'];
$opt_meta_editor = $_POST['opt_meta_editor'];
update_post_meta( $post->ID, 'txt_meta_url', $txt_meta_url);
update_post_meta( $post->ID, 'txt_meta_title', $txt_meta_title);
update_post_meta( $post->ID, 'txt_meta_size', $txt_meta_size);
update_post_meta( $post->ID, 'opt_meta_editor', $opt_meta_editor);
?>
【讨论】:
以上是关于保存元框数据问题的主要内容,如果未能解决你的问题,请参考以下文章