我正在学习元框,并已将其显示在本地开发人员上,但我不知道如何避免创建用于保存多个元框的新函数。
我假设我可以调用一个数组并重用相同的函数来生成元框内容并保存它,但我在尝试时不断出错。下面是我现在在函数文件中的代码:
/**
* Home Page Custom Meta Content
*
**/
// Add the Meta Box
function add_home_meta_box() {
add_meta_box(
\'home_meta_box\', // $id
\'Home Page Content\', // $title
\'show_home_meta_box\', // $callback
\'page\', // $page
\'normal\', // $context
\'high\'); // $priority
add_meta_box(
\'home_meta_box_lower_1\', // $id
\'Home Lower Left\', // $title
\'show_home_meta_box_lower_left\', // $callback
\'page\', // $page
\'normal\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'add_home_meta_box\');
// Creating Array for Fields
$prefix = \'home_\';
$home_meta_fields = array(
array(
\'label\' => \'Caption Title\',
\'desc\' => \'Upper section H2 caption title.\',
\'id\' => $prefix.\'title\',
\'type\' => \'text\'
),
array(
\'label\' => \'Caption Sub Title\',
\'desc\' => \'Upper section H3 caption title.\',
\'id\' => $prefix.\'sub_title\',
\'type\' => \'text\'
),
array(
\'label\'=> \'Caption\',
\'desc\' => \'Caption text block.\',
\'id\' => $prefix.\'caption\',
\'type\' => \'textarea\'
),
array(
\'name\' => \'Caption Image\',
\'desc\' => \'Upload a pre-cropped 1140px wide x 530px tall web-optimized image.\',
\'id\' => $prefix.\'image\',
\'type\' => \'image\'
)
);// end caption array
$prefix2 = \'home_lower_left\';
$home_meta_fields_lower_left = array(
array(
\'label\' => \'Column Title\',
\'desc\' => \'H2 title for column.\',
\'id\' => $prefix2.\'title\',
\'type\' => \'text\'
),
array(
\'name\' => \'Column Image\',
\'desc\' => \'Upload a pre-cropped 360px wide x 300px tall web-optimized image.\',
\'id\' => $prefix2.\'image\',
\'type\' => \'image\'
),
array(
\'label\' => \'Column Sub-Title\',
\'desc\' => \'H3 sub-title above text block.\',
\'id\' => $prefix2.\'sub_title\',
\'type\' => \'text\'
),
array(
\'label\'=> \'Text block\',
\'desc\' => \'Caption text block.\',
\'id\' => $prefix2.\'caption\',
\'type\' => \'textarea\'
),
array(
\'label\' => \'Button Label\',
\'desc\' => \'Button link label (what user reads on button).\',
\'id\' => $prefix2.\'btn_label\',
\'type\' => \'text\'
),
array(
\'label\' => \'Button URL\',
\'desc\' => \'URL where button links to. Enter http:// to work.\',
\'id\' => $prefix2.\'btn_url\',
\'type\' => \'text\'
)
);// end lower left array
//The Callback
function show_home_meta_box() {
global $home_meta_fields, $post;
// Using nonce for verification
echo \'<input type="hidden" name="home_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
//Begin field table and loop
echo \'<table class="form-table">\';
foreach ($home_meta_fields as $field) {
// get value of this field if it exists for this page
$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;
case \'image\':
$image = get_template_directory_uri().\'library/images/img-preview-blank.png\';
echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';
if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }
echo \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" />
<img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';
break;
} // end switch
echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}
// Save the Data
function save_home_meta($post_id) {
global $home_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST[\'home_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 ($home_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\', \'save_home_meta\');
// Lower Left
//The Callback
function show_home_meta_box_lower_left() {
global $home_meta_fields_lower_left, $post;
// Using nonce for verification
echo \'<input type="hidden" name="home_meta_box_left_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
//Begin field table and loop
echo \'<table class="form-table">\';
foreach ($home_meta_fields_lower_left as $field) {
// get value of this field if it exists for this page
$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;
case \'image\':
$image = get_template_directory_uri().\'library/images/img-preview-blank.png\';
echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';
if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }
echo \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" />
<img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';
break;
} // end switch
echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}
// Save the Data
function save_home_meta_lower_left($post_id) {
global $home_meta_fields_lower_left;
// verify nonce
if (!wp_verify_nonce($_POST[\'home_meta_box_left_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 ($home_meta_fields_lower_left 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\', \'save_home_meta_lower_left\');
谢谢你帮助我!非常感谢。