将代码快速放入我的测试设置中,我想您是在问为什么页面元盒会显示所需的post元输入。
您的add_meta_box()
功能。问题在于您的变量$custom_meta_fields
. 当WordPress到达文件末尾时$custom_meta_fields
由用于发布的数组定义。因此,您的页面元数据库正在使用帖子的版本$custom_meta_fields
. 这是您使用全局变量的方式的副作用。。。。它们很容易被覆盖。最简单的方法是使用不同的变量:
// Add the Meta Box
function vs_add_custom_meta_box() {
add_meta_box(
\'custom_meta_box\', // $id
\'Custom Meta Box\', // $title
\'vs_show_custom_meta_box\', // $callback
\'page\', // $page
\'normal\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'vs_add_custom_meta_box\');
// Field Array
$prefix = \'custom_\';
$custom_page_meta_fields = array(
array(
\'label\'=> \'Background Color\',
\'desc\' => \'Select the color you want to appear as the page background.\',
\'id\' => $prefix.\'select\',
\'type\' => \'select\',
\'options\' => array (
\'one\' => array (
\'label\' => \'Orange\',
\'value\' => \'blog\'
),
\'two\' => array (
\'label\' => \'Light red\',
\'value\' => \'services\'
),
\'three\' => array (
\'label\' => \'Dark red\',
\'value\' => \'works\'
),
\'four\' => array (
\'label\' => \'Green\',
\'value\' => \'portfolio\'
),
\'five\' => array (
\'label\' => \'Light blue\',
\'value\' => \'about\'
),
\'six\' => array (
\'label\' => \'Dark blue\',
\'value\' => \'contact\'
)
)
),
array(
\'label\'=> \'Main page display\',
\'desc\' => \'Tick if this page will be displayed on the homepage. || Notice: Make sure there are no 2 pages with the same color displayed on the homepage.\',
\'id\' => $prefix.\'main\',
\'type\' => \'checkbox\'
)
);
// The Callback
function vs_show_custom_meta_box() {
global $custom_page_meta_fields, $post;
// Use nonce for verification
echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
// Begin the field table and loop
echo \'<table class="form-table">\';
foreach ($custom_page_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field[\'id\'], true);
// begin a table row with
echo \'<tr>
<th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
<td>\';
switch($field[\'type\']) {
// case items will go here
// text
case \'text\':
echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// textarea
case \'textarea\':
echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea>
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// checkbox
case \'checkbox\':
echo \'<input type="checkbox" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" \',$meta ? \' checked="checked"\' : \'\',\'/>
<label for="\'.$field[\'id\'].\'">\'.$field[\'desc\'].\'</label>\';
break;
// select
case \'select\':
echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">\';
foreach ($field[\'options\'] as $option) {
echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
}
echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
} //end switch
echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}
// Save the Data
function vs_save_custom_meta($post_id) {
global $custom_page_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
// check autosave
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;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_page_meta_fields as $field) {
$old = get_post_meta($post_id, $field[\'id\'], true);
$new = $_POST[$field[\'id\']];
if ($new && $new != $old) {
update_post_meta($post_id, $field[\'id\'], $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, $field[\'id\'], $old);
}
} // end foreach
}
add_action(\'save_post\', \'vs_save_custom_meta\');
// Add the Meta Box
function vs_add_excerpt_meta_box() {
add_meta_box(
\'excerpt_meta_box\', // $id
\'Excerpt Meta Box\', // $title
\'vs_show_excerpt_meta_box\', // $callback
\'post\', // $page
\'normal\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'vs_add_excerpt_meta_box\');
// Field Array
$prefix = \'custom_\';
$custom_post_meta_fields = array(
array(
\'label\' => \'Custom post teaser\',
\'desc\' => \'Replace the default excerpt the text of your choice\',
\'id\' => $prefix.\'excerpt\',
\'type\' => \'textarea\'
)
);
// The Callback
function vs_show_excerpt_meta_box() {
global $custom_post_meta_fields, $post;
// Use nonce for verification
echo \'<input type="hidden" name="excerpt_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
// Begin the field table and loop
echo \'<table class="form-table">\';
foreach ($custom_post_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field[\'id\'], true);
// begin a table row with
echo \'<tr>
<th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
<td>\';
switch($field[\'type\']) {
// case items will go here
// text
case \'text\':
echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// textarea
case \'textarea\':
echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea>
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// checkbox
case \'checkbox\':
echo \'<input type="checkbox" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" \',$meta ? \' checked="checked"\' : \'\',\'/>
<label for="\'.$field[\'id\'].\'">\'.$field[\'desc\'].\'</label>\';
break;
// select
case \'select\':
echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">\';
foreach ($field[\'options\'] as $option) {
echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
}
echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
} //end switch
echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}
// Save the Data
function vs_save_excerpt_meta($post_id) {
global $custom_post_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST[\'excerpt_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
// check autosave
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;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_post_meta_fields as $field) {
$old = get_post_meta($post_id, $field[\'id\'], true);
$new = $_POST[$field[\'id\']];
if ($new && $new != $old) {
update_post_meta($post_id, $field[\'id\'], $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, $field[\'id\'], $old);
}
} // end foreach
}
add_action(\'save_post\', \'vs_save_custom_meta\');
您还可以移动
$custom_meta_fields
在包装器函数内部,以便可以从任何其他函数调用它。
// Add the Meta Box
function vs_add_excerpt_meta_box() {
add_meta_box(
\'excerpt_meta_box\', // $id
\'Excerpt Meta Box\', // $title
\'vs_show_excerpt_meta_box\', // $callback
\'post\', // $page
\'normal\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'vs_add_excerpt_meta_box\');
function vs_get_post_meta_fields(){
// Field Array
$prefix = \'custom_\';
return array(
array(
\'label\' => \'Custom post teaser\',
\'desc\' => \'Replace the default excerpt the text of your choice\',
\'id\' => $prefix.\'excerpt\',
\'type\' => \'textarea\'
)
);
}
// The Callback
function vs_show_excerpt_meta_box() {
global $post;
// Use nonce for verification
echo \'<input type="hidden" name="excerpt_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
// get the array with proper scope
$custom_post_meta_fields = vs_get_post_meta_fields();
// Begin the field table and loop
echo \'<table class="form-table">\';
foreach ($custom_post_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field[\'id\'], true);
// begin a table row with
echo \'<tr>
<th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
<td>\';
switch($field[\'type\']) {
// case items will go here
// text
case \'text\':
echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// textarea
case \'textarea\':
echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea>
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// checkbox
case \'checkbox\':
echo \'<input type="checkbox" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" \',$meta ? \' checked="checked"\' : \'\',\'/>
<label for="\'.$field[\'id\'].\'">\'.$field[\'desc\'].\'</label>\';
break;
// select
case \'select\':
echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">\';
foreach ($field[\'options\'] as $option) {
echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
}
echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
} //end switch
echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}
// Save the Data
function vs_save_excerpt_meta($post_id) {
$custom_post_meta_fields = vs_get_post_meta_fields();
// verify nonce
if (!wp_verify_nonce($_POST[\'excerpt_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
// check autosave
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;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_post_meta_fields as $field) {
$old = get_post_meta($post_id, $field[\'id\'], true);
$new = $_POST[$field[\'id\']];
if ($new && $new != $old) {
update_post_meta($post_id, $field[\'id\'], $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, $field[\'id\'], $old);
}
} // end foreach
}
add_action(\'save_post\', \'vs_save_custom_meta\');