我想在自定义分类下创建一个Metabox

时间:2019-06-17 作者:T alagbe

我有一个自定义的post类型和post类型下的自定义分类法。但我想在自定义分类法下添加一个元盒。我的代码不起作用,请你帮忙。

Working Code

// Register Custom Post Type
function create_book_cpt() {

$labels = array(
    \'name\'                  => _x( \'Books\', \'Post Type General Name\', \'Books\' ),
    \'singular_name\'         => _x( \'Book\', \'Post Type Singular Name\', \'Books\' ),
    \'menu_name\'             => __( \'Books\', \'Books\' ),
    \'name_admin_bar\'        => __( \'Books\', \'Books\' ),
    \'archives\'              => __( \'Item Archives\', \'Books\' ),
    \'attributes\'            => __( \'Item Attributes\', \'Books\' ),
    \'parent_item_colon\'     => __( \'Parent Item:\', \'Books\' ),
    \'all_items\'             => __( \'All Books\', \'Books\' ),
    \'add_new_item\'          => __( \'Add New Book\', \'Books\' ),
    \'add_new\'               => __( \'Add New\', \'Books\' ),
    \'new_item\'              => __( \'New Book\', \'Books\' ),
    \'edit_item\'             => __( \'Edit Book\', \'Books\' ),
    \'update_item\'           => __( \'Update Book\', \'Books\' ),
    \'view_item\'             => __( \'View Book\', \'Books\' ),
    \'view_items\'            => __( \'View Books\', \'Books\' ),
    \'search_items\'          => __( \'Search Books\', \'Books\' ),
    \'not_found\'             => __( \'Not found\', \'Books\' ),
    \'not_found_in_trash\'    => __( \'Not found in Trash\', \'Books\' ),
    \'featured_image\'        => __( \'Featured Image\', \'Books\' ),
    \'set_featured_image\'    => __( \'Set featured image\', \'Books\' ),
    \'remove_featured_image\' => __( \'Remove featured image\', \'Books\' ),
    \'use_featured_image\'    => __( \'Use as featured image\', \'Books\' ),
    \'insert_into_item\'      => __( \'Insert into Book\', \'Books\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded to this Book\', \'Books\' ),
    \'items_list\'            => __( \'Books list\', \'Books\' ),
    \'items_list_navigation\' => __( \'Books list navigation\', \'Books\' ),
    \'filter_items_list\'     => __( \'Filter Books list\', \'Books\' ),
);
$args = array(
    \'label\'                 => __( \'Book\', \'Books\' ),
    \'description\'           => __( \'Open Books\', \'Books\' ),
    \'labels\'                => $labels,
    \'menu_icon\'=>\'dashicons-editor-video\',
    \'supports\'              => array( \'title\'),
    \'hierarchical\'          => false,
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'menu_position\'         => 5,
    \'show_in_admin_bar\'     => true,
    \'show_in_nav_menus\'     => true,
    \'can_export\'            => true,
    \'has_archive\'           => true,
    \'exclude_from_search\'   => false,
    \'publicly_queryable\'    => true,
    \'capability_type\'       => \'post\',
);
register_post_type( \'Book\', $args );}
  add_action( \'init\', \'create_Book_cpt\', 0 );

     function add_Book_series_taxonomy(){

$labels = array(
    \'name\'               => \'Series\',
    \'singular_name\'      => \'Series\',
    \'search_items\'       => \'Search Series\',
    \'all_items\'          => \'All Series\',
    \'update_item\'        => \'Update Series\',
    \'edit_item\'          => \'Edit Series\',
    \'add_new_item\'       => \'Add New Series\',
    \'new_item_name\'      => \'New Series Name\',
    \'menu_name\'          => \'Series\'
);

//define arguments to be used
$args = array(
    \'labels\'            => $labels,
    \'hierarchical\'      => true,
    \'show_ui\'           => true,
    \'how_in_nav_menus\'  => true,
    \'public\'            => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
    \'rewrite\'           => array(\'slug\' => \'Series\')
);

//call the register_taxonomy function
register_taxonomy(\'Series\', \'Book\', $args);}
     add_action(\'init\',\'add_Book_series_taxonomy\');

Not Working

function add_Book_series_metabox()
 {
   add_meta_box("series_details",\'Series 
   Details\',\'Book_series_details\',\'Series\');
   }

  function Book_series_details($term)
   {
     wp_nonce_field(\'open_Book_save_series_meta\',\'open_Book_details_nonce\');

      $term_meta = get_option($term->id,\'_Book_series_value\',true);

     ?>
      <label for="series_details"> Series Details</label>
     <input type="text" id="series_details" name="series_details" value="<? 
       php _e($term_meta,\'Book\') ?>">

      <?php

        }
       add_action(\'add_meta_boxes\',\'add_Book_series_metabox\');
谢谢你

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

我可以通过使用此函数来修复它

add_action( \'series_add_form_fields\', \'sermon_series_add_details\', 10, 2 );
而不是这样:

add_action(\'add_meta_boxes\',\'add_Book_series_metabox\');

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: