如何构建多个元盒

时间:2011-06-25 作者:mmaximalist

到目前为止,我已经有了一个针对单个元框的完美工作脚本。(我将提供以下详细信息。)然而,我想添加更多的元框,但在思考如何弯曲或添加到此脚本以获得这些结果时遇到了困难。

有人向我解释说,我应该为每个元盒创建单独的include,并从本质上重新创建我已经拥有的内容的副本,更改我的功能,例如:plib_add_boxplib_add_box_2 以此类推,以避免冲突。但这太粗糙了,我知道必须有一种方法可以简单地将其添加到现有的include中,并基本上保持格式/保存数据部分不变。

我的functions.php:

include("metaboxes/preset-library.php");

//I created an array called $meta_box and set the array key to the relevant post type, in this case post
$meta_box[\'post\'] = array(
    \'id\' => \'venue_location\',  
    \'title\' => \'Venue/Location\',
    \'context\' => \'normal\',
    \'priority\' => \'high\',
    \'fields\' => array(
        array(
            \'name\' => \'Venue\',
            \'desc\' => \'Venue Name\',
            \'id\' => \'venue_info\',
            \'type\' => \'text\',
            \'default\' => \'\'
        ),
        array(
            \'name\' => \'Location\',
            \'desc\' => \'Location of the Venue\',
            \'id\' => \'location_info\',
            \'type\' => \'text\',
            \'default\' => \'\'
        )
    )
);
add_action(\'admin_menu\', \'plib_add_box\');
我的include:

//Add meta boxes to post types
function plib_add_box() {
    global $meta_box;

    foreach($meta_box as $post_type => $value) {
        add_meta_box($value[\'id\'], $value[\'title\'], \'plib_format_box\', $post_type, $value[\'context\'], $value[\'priority\']);
    }
}
//Formatting
function plib_format_box() {
  global $meta_box, $post;

  // verification
  echo \'<input type="hidden" name="plib_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)), \'" />\';

  echo \'<table class="form-table">\';

  foreach ($meta_box[$post->post_type][\'fields\'] as $field) {
      // get current post meta data
      $meta = get_post_meta($post->ID, $field[\'id\'], true);

      echo \'<tr>\'.
              \'<th style="width:20%"><label for="\'. $field[\'id\'] .\'">\'. $field[\'name\']. \'</label></th>\'.
              \'<td>\';
      switch ($field[\'type\']) {
          case \'text\':
              echo \'<input type="text" name="\'. $field[\'id\']. \'" id="\'. $field[\'id\'] .\'" value="\'. ($meta ? $meta : $field[\'default\']) . \'" size="30" style="width:97%" />\'. \'<br />\'. $field[\'desc\'];
              break;
          case \'textarea\':
              echo \'<textarea name="\'. $field[\'id\']. \'" id="\'. $field[\'id\']. \'" cols="60" rows="4" style="width:97%">\'. ($meta ? $meta : $field[\'default\']) . \'</textarea>\'. \'<br />\'. $field[\'desc\'];
              break;
          case \'select\':
              echo \'<select name="\'. $field[\'id\'] . \'" id="\'. $field[\'id\'] . \'">\';
              foreach ($field[\'options\'] as $option) {
                  echo \'<option \'. ( $meta == $option ? \' selected="selected"\' : \'\' ) . \'>\'. $option . \'</option>\';
              }
              echo \'</select>\';
              break;
          case \'radio\':
              foreach ($field[\'options\'] as $option) {
                  echo \'<input type="radio" name="\' . $field[\'id\'] . \'" value="\' . $option[\'value\'] . \'"\' . ( $meta == $option[\'value\'] ? \' checked="checked"\' : \'\' ) . \' />\' . $option[\'name\'];
              }
              break;
          case \'checkbox\':
              echo \'<input type="checkbox" name="\' . $field[\'id\'] . \'" id="\' . $field[\'id\'] . \'"\' . ( $meta ? \' checked="checked"\' : \'\' ) . \' />\';
              break;
      }
      echo     \'<td>\'.\'</tr>\';
  }

  echo \'</table>\';

}
// Save data from meta box
function plib_save_data($post_id) {
    global $meta_box,  $post;

    //Verify
    if (!wp_verify_nonce($_POST[\'plib_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;
    }

    foreach ($meta_box[$post->post_type][\'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);
        }
    }
}

add_action(\'save_post\', \'plib_save_data\');

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

您可以创建任意多个元数据库。使用函数创建它们,然后将其挂接到init。第一行应该是$meta_boxes = array(); 然后,使用$meta_boxes[] = array(

add_action( \'init\', \'prefix_create_metaboxes\' );
function prefix_create_metaboxes() {

$meta_boxes = array();
$meta_boxes[] = array(
    \'id\' => \'venue_location\',  
    \'title\' => \'Venue/Location\',
    \'pages\' => array(\'post\'), // post type
    \'context\' => \'normal\',
    \'priority\' => \'high\',
     array(
        \'name\' => \'Venue\',
        \'desc\' => \'Venue Name\',
        \'id\' => \'venue_info\',
        \'type\' => \'text\',
        \'default\' => \'\'
    ),
    array(
        \'name\' => \'Location\',
        \'desc\' => \'Location of the Venue\',
        \'id\' => \'location_info\',
        \'type\' => \'text\',
        \'default\' => \'\'
    ),
)
);
$meta_boxes[] = array(
    \'id\' => \'another_meta_box\',
    \'title\' => \'Another Meta Box Title\',
    \'pages\' => array( \'post\' ), // post type
    \'context\' => \'side\',
    \'priority\' => \'low\',
    \'show_names\' => true, 
    \'fields\' => array(
    array(
        \'name\' => \'Field Name\',
        \'desc\' => \'Field Desc.\',
        \'id\' => \'field_id\',
        \'type\' => \'text\'
    ),
    )
    );
}

结束

相关推荐

自定义发布类型Metabox-不保存

我添加了一个自定义的post类型,效果很好;我还添加了两个元数据库,它们似乎工作得很好,但它们中的内容总是在几分钟后消失。如果有人能在这方面提供帮助,我将万分感激,S。//元框代码//add_action( \'admin_init\', \'add_custom_metabox\' ); add_action( \'save_post\', \'save_custom_details\' ); function add_custom_metabox() { ad