为什么元框没有出现在WordPress中添加新UI?

时间:2015-07-27 15:05:35

标签: php wordpress custom-post-type

我的代码如下(我正在处理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.
?>

我已经多次将它与tuts +教程进行了比较,因为我的生活看不出我做错了什么......帮帮忙!

2 个答案:

答案 0 :(得分:1)

替换此行:

$(document).ready(function () {
  $('.tab-menu li a').click(function () {
    $('.tab-menu li a').removeClass('active');
    $(this).addClass('active');
    tab = $(this).attr('href');
    $('.tabs .active').removeClass('active');
    $(tab).addClass('active');
  });
});

使用:

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

我们将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 与自定义帖子类型$screen匹配。

或者替换:

contact

使用:

 register_post_type( 'contact', array(

答案 1 :(得分:1)

我知道答案已被接受,但我只是想提一下CMB2库。我使用它,它比标准的Wordpress方式更好,更容易。 我知道其他流行的插件使用它作为代码的基础。

可以在GitHub上找到: https://github.com/webdevstudios/CMB2