Why is meta box not appearing in WordPress Add New

2019-09-02 03:40发布

问题:

My code is as follows (I'm working off this tuts+ tutorial):

<?php
/**
* Plugin Name: Sermon Manager Plus
* Plugin URI: #
* Version: 0.1
* Author: Dave Mackey
* Author URI: http://www.davemackey.net/
* Description: A robust system for sermon management.
* License: GPL2
*/
?>
<?php
  class SermonManagerPlus {

  /**
   * Constructor: Called when plugin is initialized.
   */
   function __construct() {
    add_action( 'init', array( $this, 'register_custom_post_type' ) );   
    add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );
   }

  /**
 * Registers a Custom Post Type called series
 */
  function register_custom_post_type() {
     register_post_type( 'contact', array(
      'labels' => array(
          'name'        => _x( 'Series', 'post type general name', 'sermon-manager-plus' ), // Plural name of CPT
          'singular_name' => _x( 'Series', 'post type singular name', 'sermon-manager-plus' ), // Singular name of CPT
          'menu_name' => _x( 'Sermons', 'admin menu', 'sermon-manager-plus' ), // Name that appears on the admin menu.
          'name_admin_bar' => _x( 'Series', 'sermon-manager-plus'),  // Name that appears on top fixed admin menu under new.
          'add_new' => _x( 'Add New', 'series', 'sermon-manager-plus'), // Add in admin menu under menu_name.
          'add_new_item' => __( 'Add New Series', 'sermon-manager-plus'),
          'new_item' => __( 'New Series', 'sermon-manager-plus'),
          'edit_item' => __( 'Edit Series', 'sermon-manager-plus'),
          'view_item' => __( 'View Series', 'sermon-manager-plus'),
          'all_items' => __( 'All Series', 'sermon-manager-plus'),
          'search_items' => __( 'Search Series', 'sermon-manager-plus'),
          'parent_item_colon' => __( 'Parent Series:', 'sermon-manager-plus'), // Only used with hierarchical CPT.
          'not_found' => __( 'No Series Found.', 'sermon-manager-plus'),
          'not_found_in_trash' => __('No Series Found in Trash.', 'sermon-manager-plus'),
          ),

          // Frontend
          'has_archive' => false, // Specific archive template?
          'public'  => false, // Can be seen by public?
          'public_queryable' => false, // Queryable by public?

          // Admin
          'capability_type' => 'post', // Use permissions as those used for post.
          'menu_icon' => 'dashicons-info', // See developer.wordpress.org/resource/dashicons/
          'menu_position' => 10,  // Appears below this menu item at the given value: 5 (posts), 10 (media), 15 (links), 20 (pages), 25 (comments)
          // 60 (first separator), 65 (plugins), 70 (users), 75 (tools), 80 (settings), 100 (second separator)
          'query_var' => true, // Query variable is set to post type.
          'show_in_menu' => true, // can be false (don't show), true (show top-level), or a string like 'tools.php' or 'edit.php?post_type="page"', e.g. if you want
          // to nest this CPT under another CPT, for example Sermons nested under Series.
          'show_ui' => true, // Generates a default UI for managing post type in admin.
          'supports' => array( // Options: title, editor, author, thumbnail, excerpt, trackbacks, custom-fields, comments, revisions, page-attributes, post-formats
              'title',
              'author',
              'comments'
              ),
              ) );
}
/**
 * Registers a Meta Box on our Series Custom Post Type, called 'Summary'
 */
function register_meta_boxes() {
    // See: https://codex.wordpress.org/Function_Reference/add_meta_box
    add_meta_box( 'series-summary', 'Series Summary', array( $this, 'output_meta_box' ), 'series', 'normal', 'high'); // $id, $title, $callback, $screen, $context, $priority
}
/**
 * Output a Series Summary meta box
 * 
 * @param WP_Post $post WordPress Post object
 */
 function output_meta_box ( $post ) {
     // Output label and field
     echo ( '<label for="series_summary">' . __( 'Summary', 'sermon-manager-plus' ) . '</label>' );
     echo ( '<input type="text" name="series_summary" id="series_summary" value="' . esc_attr( $email ) . '" />' );
 }
}


$SermonManagerPlus = new SermonManagerPlus; // Create an object of type SermonManagerPlus.
?>

I've compared it several times to the tuts+ tutorial and for the life of me can't see what I'm doing wrong...Help!

回答1:

Replace this line:

add_meta_box( 
    'series-summary', 
    'Series Summary', 
    array( $this, 'output_meta_box' ), 
    'series', // <-- Problem here!
    'normal', 
    'high'
); // $id, $title, $callback, $screen, $context, $priority

with:

add_meta_box( 
    'series-summary', 
    'Series Summary', 
    array( $this, 'output_meta_box' ), 
    'contact',  //<-- Match it with the post type 
    'normal', 
    'high'
); // $id, $title, $callback, $screen, $context, $priority

where we match the $screen with the custom post type contact.

Alternatively replace:

 register_post_type( 'contact', array(

with:

 register_post_type( 'series', array(


回答2:

I know the answer was accepted, but I just want to mention the CMB2 library. I used it and it is great, much easier than the standard Wordpress way. And I understand that other popular plugins use it as base for their code.

It can be found on GitHub: https://github.com/webdevstudios/CMB2