开发者

Wordpress add_meta_box not working in custom post type when in a Class

I am having a pro开发者_开发问答blem getting meta boxes into custom post types that are created with a plugin.

I have gone through the code a hundred times and am starting to wonder wether the Class is having an impact, does array($this, 'insT_tag') work as a callback reference?

I really can't see what could be wrong! I have a sample of the code I am using to create the type and then add the meta box. Does anyone have any ideas?

function init_custom_post_types() {

  register_post_type('inGallery', array(
   'labels' => array(
     'name' => __('inGalleries', 'inGallery'),
     'singular_name' => 'inGallery',
     'add_new' => 'Add new inGallery',
     'add_new_item' => 'Add new inGallery',
     'edit_item' => 'Edit inGallery',
     'new_item' => 'New inGallery',
     'view_item' => 'Show inGallery',
     'search_items' => 'Search inGallery',
     'not_found' =>  'Not found',
     'not_found_in_trash' => 'No inGallery was found in trash',
     'parent_item_colon' => 'Parent:'
   ),
   'public' => true,
   'exclude_from_search' => false,
   'query_var' => true,
   'capability_type' => 'post',
   'show_in_nav_menus' => true,
   'menu_position' => 5,
   'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions'),
   'taxonomies' => array('category', 'post_tag'),
   'rewrite' => array( 'slug' => 'in', 'with_front' => false ),
   'publicly_queryable' => true,
   'exclude_from_search' => false,
   'can_export' => true,
   'register_meta_box_cb' => array($this , 'add_inGallery_metaboxes' )
  ));

// Add the custom type to the homepage post loop
  add_filter( 'pre_get_posts', array($this, 'customTypeToPosts'));

}

public function add_inGallery_metaboxes () {
    add_meta_box( 'insT_tag', 'Hash Tag', array($this, 'insT_tag'), 'inGallery', 'side', 'high' , array('tester'));
}

public function insT_tag ( $a ) {
    print_r($a);
    echo'<input type="text" value="tester"/>';
}


So after many hours I have found an answer… and it's horribly simple. It seems the post type name gets converted to lowercase, so my camelCase name inGallery became ingallery. That simple!

Also worth noting a good way to debug this was to run something like :

foreach ( $post_types as $post_type ) {
    echo "<p>" . $post_type . '</p>';
}

So you can see what the references are called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜