在这种情况下,您需要在特定的元框中找到钩子,并将自定义帖子类型添加到其pages
数组,例如:
$meta_boxes[\'test_metabox\'] = array(
\'id\' => \'test_metabox\',
\'title\' => \'Test Metabox\',
\'pages\' => array(\'post\',\'page\',\'artists\'), //list custom post types here!
\'context\' => \'normal\',
\'priority\' => \'high\',
Meta-Box插件本质上添加了一个类,使元盒的编码更加容易。您仍然需要将元盒本身注册并编码到主题文件中(请参见下面的示例)。要仅在自定义帖子类型上显示,您需要修改
pages
数组(要添加
\'artists\'
到如上所示的阵列)。
代码示例(未测试)
假设您已经安装并激活了插件,您应该能够通过将以下代码粘贴到主题中来添加一些文本区域和复选框元框
functions.php
文件:
add_filter( \'rwmb_meta_boxes\', \'t129292_register_meta_boxes\' );
function t129292_register_meta_boxes( $meta_boxes ) {
$prefix = \'rw_\';
// Register the metabox
$meta_boxes[] = array(
\'id\' => \'personal\',
\'title\' => \'Personal Information\',
\'pages\' => array( \'artists\' ), //displays on artists post type only
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(
// add a text area
array(
\'name\' => \'Text Area\',
\'desc\' => \'Description\',
\'id\' => $prefix . \'text1\',
\'type\' => \'textarea\',
\'cols\' => 20,
\'rows\' => 3,
),
// add another text area
array(
\'name\' => \'Text Area\',
\'desc\' => \'Description\',
\'id\' => $prefix . \'text2\',
\'type\' => \'textarea\',
\'cols\' => 20,
\'rows\' => 3
),
// CHECKBOX
array(
\'name\' => __( \'Checkbox\', \'rwmb\' ),
\'id\' => $prefix . \'checkbox\',
\'type\' => \'checkbox\',
// Value can be 0 or 1
\'std\' => 1,
)
)
);
return $meta_boxes;
}
然后,要在主题文件的前端显示选项,可以使用helper函数,例如。
echo rwmb_meta( \'rw_text1\' ); //echo the contents from the first textarea
echo rwmb_meta( \'rw_text2\' ); //echo the contents from the second textarea
echo rwmb_meta( \'rw_checkbox\' ); //echo 1 or 0 if the checkbox is checked
这些示例是根据插件的
Github repo 其中演示文件(链接)包含关于使用各种元盒类型的代码,而不仅仅是文本区域和复选框。