我在写文章时使用了多个metabox面板。在我的函数中,我注册了多个元数据库:
$prefix = \'dbt_\';
$meta_boxes = array();
$meta_boxes[] = array(
\'id\' => \'general_information\',
\'title\' => \'General Information\',
\'pages\' => array(\'post\', \'page\', \'link\'), // multiple post types, accept custom post types
\'context\' => \'normal\', // normal, advanced, side (optional)
\'priority\' => \'high\', // high, low (optional)
\'fields\' => array(
array(
\'name\' => \'Name\',
\'id\' => $prefix . \'name\',
\'type\' => \'text\',
\'std\' => \'default value here\'
),
array(
\'name\' => \'Manufacturer\',
\'id\' => $prefix . \'manufacturer\',
\'type\' => \'text\',
\'std\' => \'\'
)
$meta_boxes[] = array(
\'id\' => \'measurements\',
\'title\' => \'Measurements\',
\'pages\' => array(\'post\', \'page\', \'link\'), // multiple post types, accept custom post types
\'context\' => \'normal\', // normal, advanced, side (optional)
\'priority\' => \'high\', // high, low (optional)
\'fields\' => array(
array(
\'name\' => \'Length\',
\'id\' => $prefix . \'length\',
\'type\' => \'text\',
\'std\' => \'\'
),
array(
\'name\' => \'Width\',
\'id\' => $prefix . \'width\',
\'type\' => \'text\',
\'std\' => \'\'
)
..etc
在我的主题中,我将显示每个metabox组中每个字段的名称和值。目标是让字段按元盒分组,否则我只会使用
get_post_custom($post_id)
.
单一代码。php:
global $meta_boxes;
foreach ( $meta_boxes as $metabox ) {
echo $metabox[\'title\'];
foreach ( $metabox[\'fields\'] as $field ) {
$meta = get_post_meta($post->ID, $field[\'id\'], true); //get post meta from each metabox
if(!isset($meta[0])) continue; //display only fields with values
echo $field[\'name\'];
echo $meta ? $meta : $field[\'std\']; //show value or default value
}
}
例如,如果元框没有带值的字段,则元框标题
$metabox[\'title\']
仍然显示。如果不存在值,我如何隐藏metabox标题?