我正在与一个食谱博客合作,我使用自定义元框。
$food_box = array(
\'id\' => \'foodbox\',
\'title\' => __(\'Ingredients details\',\'splus\'),
\'page\' => \'recipes\',
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(),);
$arr = array ( \'name\' => \'-\', \'desc\' => \'-\', \'id\' => \'-\',\'type\' => \'text\', \'std\' => \'\');
add_action(\'admin_menu\', \'mytheme_add_box\'); // Add meta box
function mytheme_add_box() {
global $meta_box;
global $food_box;
global $test_box;
add_meta_box(
$meta_box[\'id\'],
$meta_box[\'title\'],
\'recipe_details_show_box\',
$meta_box[\'page\'],
$meta_box[\'context\'],
$meta_box[\'priority\']
);
add_meta_box(
$food_box[\'id\'],
$food_box[\'title\'],
\'ingredients_show_box\',
$food_box[\'page\'],
$food_box[\'context\'],
$food_box[\'priority\']
);
}
A根据配方将配料分为几组食物,同一食物可以在一个配方中出现两次(例如,面团中的糖为200克,馅料中的糖为50克)。为了结合两种分类法(食物项目和食物集),我创建了自定义表并添加了文件
$food_box[\'fields\'][] = $arr;
按下一代码从此表中删除:
foreach ( $terms as $term ) { // show tabs content for foodset
$s = ($i==1) ? \' \': \' style="display: none;"\';
echo \'<div id="set-\'.$term->term_id .\'" class="tabs-panel"\' . $s . \'>\';
echo \'<ul id="set2-checklist" class="categorychecklist form-no-clear" >\';
echo \'<table id="food_data_table_header" class="form-table" border="0" align="left">\';
echo \'<tr>\'; //show header
echo \'<td style="width: 50%" > <span>FoodSet</span> </td>\';
echo \'<td style="width: 50%" > <span>FoodId</span> </td>\';
echo \'<td style="width:100%" > <span>Description</span> </td>\';
echo \'<td style="width: 50%" > <span>FoodUnit</span> </td>\';
echo \'<td style="width: 50%" > <span>Quantity</span> </td>\';
echo \'<td style="width: 50%" > <span>Order</span> </td>\';
echo \'<td style="width:100%" > <span>Comment</span> </td>\';
echo \'</tr>\';
foreach ( $foods as $food ) { //show data
$food_row = $wpdb->get_row($wpdb->prepare(
"SELECT rf.post_id, rf.foodset_id, rf.food_id, tt1.description, rf.food_unit, rf.quantity, rf.food_order, rf.food_comment
FROM $wpdb->term_taxonomy tt1 JOIN $wpdb->recipefood rf ON rf.food_id = tt1.term_id
WHERE (rf.post_id=".$post_id.") AND (rf.foodset_id=".$term->term_id.") AND (rf.food_id=".$food->term_id.")"));
$food_set = $term->term_id;
$food_id = $food->term_id;
$id = $food_set . \'-\' . $food_id;
$food_name = $food_row->description;
echo \'<tr>\';
echo \'<td>\',$food_set,\'</td>\';
echo \'<td>\',$food_id,\'</td>\';
echo \'<td>\',$food_name,\'</td>\';
$fls = array (\'food_unit\',\'quantity\',\'food_order\',\'food_comment\');
foreach ($fls as $fl) {
switch ($fl) {
case \'food_unit\':
$def_val = $def_unit; break;
case \'quantity\':
$def_val = $def_qty; break;
case \'food_order\':
$def_val = \'1\'; break;
case \'food_comment\':
$def_val = \'comment\'; break;
}
$meta = ($food_row->$fl == \'\') ? $def_val : $food_row->$fl;
$arr = array ( \'name\' => $fl.\'-\'. $id, \'desc\' => $fl.\'-\'. $id, \'id\' => $fl.\'-\'. $id,\'type\' => \'text\', \'std\' => $meta);
$food_box[\'fields\'][] = $arr; //add new field to array
echo \'<td>\',\'<input type="text" name="\'.$fl.\'-\'.$id. \'" id="\'.$fl.\'-\'.$id.\'" value="\',$meta , \'" style="width: 50%"/>\', \'</td>\';
// $meta ? $meta : $food_box[$fl.\'-\'.$id][\'std\']
}
echo \'</tr>\';
但很遗憾,我无法将更新的数据保存为
$_POST[\'quantity-708-481\']
为空且未在中定义
add_action(\'save_post\', \'mytheme_save_data\'); // Save recipe post from meta box
function mytheme_save_data($post_id) {
global $meta_box, $food_box;
//...
$wpdb->update( $wpdb->recipefood,
array( //set
\'food_comment\' => $_POST[\'quantity-708-481\']
), array( //where
\'post_id\' => \'1\',
\'foodset_id\' => \'822\',
\'food_id\' => \'484\'
)
);
换句话说,该字段添加到元框中,但在保存期间不可见。任何建议都是非常受欢迎的。