我使用Wordpress参考中的代码制作了一个自定义元数据库:
http://codex.wordpress.org/Function_Reference/add_meta_box
<?php
/* Define the custom box */
// WP 3.0+
// add_action(\'add_meta_boxes\', \'myplugin_add_custom_box\');
// backwards compatible
add_action(\'admin_init\', \'myplugin_add_custom_box\', 1);
/* Do something with the data entered */
add_action(\'save_post\', \'myplugin_save_postdata\');
/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
\'myplugin_sectionid\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'post\'
);
add_meta_box(
\'myplugin_sectionid\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'page\'
);
}
/* Prints the box content */
function myplugin_inner_custom_box() {
// Use nonce for verification
wp_nonce_field( plugin_basename(__FILE__), \'myplugin_noncename\' );
// The actual fields for data entry
echo \'<label for="myplugin_new_field">\';
_e("Description for this field", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />\';
}
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename(__FILE__) ) )
return $post_id;
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( \'page\' == $_POST[\'post_type\'] )
{
if ( !current_user_can( \'edit_page\', $post_id ) )
return $post_id;
}
else
{
if ( !current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
// OK, we\'re authenticated: we need to find and save the data
$mydata = $_POST[\'myplugin_new_field\'];
// Do something with $mydata
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
return $mydata;
}
?>
我不知道如何在每页上显示它的值?
<?php
$meta = get_post_meta($post->ID, \'myplugin_new_field\');
var_dump($meta);
?>
提供:
数组(0){}
此外,在单击更新帖子/或页面后,metabox字段不会更新其值。这是因为输入值=“whathever”,而它应该类似于上面的代码。
有什么想法吗?
非常感谢。
[编辑]
未更新输入值的最终代码:
/* Define the custom box */
add_action(\'add_meta_boxes\', \'myplugin_add_custom_box\');
// backwards compatible
add_action(\'admin_init\', \'myplugin_add_custom_box\', 1);
/* Do something with the data entered */
add_action(\'save_post\', \'myplugin_save_postdata\');
/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
\'metabox_sidebar_select\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'post\'
);
add_meta_box(
\'metabox_sidebar_select\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'metabox_sidebar_select\',
\'page\'
);
}
/* Prints the box content */
function myplugin_inner_custom_box() {
// Use nonce for verification
wp_nonce_field( plugin_basename(__FILE__), \'myplugin_noncename\' );
// The actual fields for data entry
echo \'<label for="myplugin_new_field">\';
_e("Description for this field", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="\'.get_post_meta($post->ID, \'myplugin_new_field\',true).\'" size="25" />\';
}
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename(__FILE__) ) )
return $post_id;
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( \'page\' == $_POST[\'post_type\'] )
{
if ( !current_user_can( \'edit_page\', $post_id ) )
return $post_id;
}
else
{
if ( !current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
// OK, we\'re authenticated: we need to find and save the data
$mydata = $_POST[\'myplugin_new_field\'];
// Do something with $mydata
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
global $post;
update_post_meta($post->ID, myplugin_new_field, $mydata);
return $mydata;
}
最合适的回答,由SO网友:Bainternet 整理而成
答案就在你的代码里,上面写着:
//可能使用add\\u post\\u meta()、update\\u post\\u meta()或自定义表来处理$mydata(请参阅下面的进一步阅读部分)
您需要实际将数据插入/更新到数据库中,因此需要添加以下内容:
global $post;
update_post_meta($post->ID, \'myplugin_new_field\', $mydata);
数据将被保存,以便您可以使用代码获取:
$meta = get_post_meta($post->ID, \'myplugin_new_field\');
您需要的唯一其他更改是在显示metabox更改的函数中:
echo \'<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />\';
至
echo \'<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="\'.get_post_meta($post->ID, \'myplugin_new_field\',true).\'" size="25" />\';
更新时间:
回答评论中的问题:
要保存选择列表,只需将其保存为文本输入,要在元框中显示以前选择的选项,只需循环该选项,查看所选内容,然后添加“selected”属性。例如:
//get last selected value if exists
$selected = get_post_meta($post->ID, \'myplugin_new_select_field\',true);
echo \'<select name="myplugin_new_select_field">\';
$x = 0;
while ($x < 4){
echo \'<option value="\'.$x.\'"\';
//check if the last value is the same is current value.
if ($x == $selected)
echo \' selected="selected"\';
echo \'>\'.$x.\'</option>\';
}
echo \'</select>\';