插件“meta box”:在自定义贴子类型中实现meta box

时间:2014-03-15 作者:Kent Miller

我是一个WP初学者,看到了“Meta Box”插件的潜力(http://wordpress.org/plugins/meta-box/). 我已经激活了插件。到目前为止,我设法上传了演示。并在博客文章窗口中显示各种元框。

我已经阅读了文档,但仍然不知道如何注册自定义帖子类型的元框。如何实现它们?

到目前为止,我的自定义帖子类型(关于列出艺术家)的代码如下所示:

add_action(\'init\', \'add_cpt_artists\');
function add_cpt_artists() {
$labels = array(
      \'name\' => _x(\'Artists\', \'post type general name\'),
      \'singular_name\' => _x(\'Artist\', \'post type singular name\'),
      \'add_new\' => _x(\'Add\', \'artist\'),
      \'add_new_item\' => __(\'Add new artist\'),
      \'edit_item\' => __(\'Edit artist\'),
      \'new_item\' => __(\'New artist\'),
      \'view_item\' => __(\'View artist\'),
      \'search_items\' => __(\'Search for artist\'),
      \'not_found\' =>  __(\'No artist found\'),
      \'not_found_in_trash\' => 
      __(\'No artist in trash\'),
      \'parent_item_colon\' => \'\'
   );
$supports = array(\'title\', \'editor\', \'thumbnail\', \'excerpt\');
$args = array(
      \'labels\' => $labels,
      \'public\' => true,
      \'publicly_queryable\' => true,
      \'show_ui\' => true, 
      \'_builtin\' => false,
      \'show_in_menu\' => true, 
      \'query_var\' => true,
      \'rewrite\' => array("slug" => "artists"),
      \'capability_type\' => \'post\',
      \'hierarchical\' => false,
      \'has_archive\' => true, 
      \'hierarchical\' => false,
      \'menu_position\' => 10,
      \'supports\' => $supports
   );
register_post_type(\'artists\',$args);
}
假设我想从Meta Box插件中添加两个文本区域和一个带有复选框的框。

在哪里插入代码

2 个回复
最合适的回答,由SO网友:MBL 整理而成

在这种情况下,您需要在特定的元框中找到钩子,并将自定义帖子类型添加到其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 其中演示文件(链接)包含关于使用各种元盒类型的代码,而不仅仅是文本区域和复选框。

SO网友:Max

This tutorial 可能会有帮助。

然后,您可以使用get_post_meta()

结束