Wordpress:我的自定义元框保存在自定义字段部分


Wordpress: My Custom Meta Box is saving in the Custom Fields Section

我 http://www.sitepoint.com/adding-custom-meta-boxes-to-wordpress/使用了此页面中的教程,当我保存帖子时,它将输入的信息放入Wordpress的自定义字段中。我修改了代码以仅适用于帖子类型 = 页面,并且仅适用于特定页面模板。到目前为止一切正常,除了保存时,它不会保存在键入它的框中。

<?php
/**
*Members Page Custom Fields
**/
/* Create one or more meta boxes to be displayed on the post editor screen. */
function custom_meta_box_markup() {
  wp_nonce_field(basename(__FILE__), "meta-box-nonce");
  ?>
      <div>
          <label for="meta-box-text">Text</label>
          <input name="meta-box-text" type="text" value="<?php echo get_post_meta($object->ID, "meta-box-text", true); ?>">
          <br>
          <label for="meta-box-dropdown">Dropdown</label>
          <select name="meta-box-dropdown">
              <?php 
                  $option_values = array(1, 2, 3);
                  foreach($option_values as $key => $value) 
                  {
                      if($value == get_post_meta($object->ID, "meta-box-dropdown", true))
                      {
                          ?>
                              <option selected><?php echo $value; ?></option>
                          <?php    
                      }
                      else
                      {
                          ?>
                              <option><?php echo $value; ?></option>
                          <?php
                      }
                  }
              ?>
          </select>
          <br>
          <label for="meta-box-checkbox">Check Box</label>
          <?php
              $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true);
              if($checkbox_value == "")
              {
                  ?>
                      <input name="meta-box-checkbox" type="checkbox" value="true">
                  <?php
              }
              else if($checkbox_value == "true")
              {
                  ?>  
                      <input name="meta-box-checkbox" type="checkbox" value="true" checked>
                  <?php
              }
          ?>
      </div>
  <?php  
    
}
function add_custom_meta_box() {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
    // check for a template type
    if ($template_file == 'members-page.php') {
      add_meta_box(
        "demo-meta-box", 
        "Custom Meta Box", 
        "custom_meta_box_markup", 
        "page", 
        "side", 
        "high", 
        null
      );
    }
}
add_action("add_meta_boxes", "add_custom_meta_box");
function save_custom_meta_box($post_id, $post, $update)
{
    if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__)))
        return $post_id;
    if(!current_user_can("edit_post", $post_id))
        return $post_id;
    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return $post_id;
    $slug = "page";
    if($slug != $post->post_type)
        return $post_id;
    $meta_box_text_value = "";
    $meta_box_dropdown_value = "";
    $meta_box_checkbox_value = "";
    if(isset($_POST["meta-box-text"]))
    {
        $meta_box_text_value = $_POST["meta-box-text"];
    }   
    update_post_meta($post_id, "meta-box-text", $meta_box_text_value);
    if(isset($_POST["meta-box-dropdown"]))
    {
        $meta_box_dropdown_value = $_POST["meta-box-dropdown"];
    }   
    update_post_meta($post_id, "meta-box-dropdown", $meta_box_dropdown_value);
    if(isset($_POST["meta-box-checkbox"]))
    {
        $meta_box_checkbox_value = $_POST["meta-box-checkbox"];
    }   
    update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value);
}
add_action("save_post", "save_custom_meta_box", 10, 3);

您的代码function custom_meta_box_markup() .

它应该function custom_meta_box_markup( $object ).