I think you’ll have to register two separate boxes in order to do that. Something like this if you don’t want to duplicate all the field code:
<?php
add_action( 'cmb2_init', 'yourprefix_register_cpt_metaboxs' );
/**
* Hook in and add a metabox that only appears on the 'About' page
*/
function yourprefix_register_cpt_metaboxs() {
$cmb_books = new_cmb2_box( array(
'id' => '_yourprefix_book_cpt_metabox',
'title' => __( 'Books Metabox', 'cmb2' ),
'object_types' => array( 'book' ),
'show_in_rest' => WP_REST_Server::READABLE, // show in REST API
) );
yourprefix_register_shared_cpt_fields( $cmb_books );
$cmb_magazines = new_cmb2_box( array(
'id' => '_yourprefix_magazine_cpt_metabox',
'title' => __( 'Magazines Metabox', 'cmb2' ),
'object_types' => array( 'magazine' ),
'show_in_rest' => false, // No REST API for this cpt
) );
yourprefix_register_shared_cpt_fields( $cmb_magazines );
}
function yourprefix_register_shared_cpt_fields( $cmb ) {
$cmb->add_field( array(
'name' => __( 'Publisher', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => '_yourprefix_publisher',
'type' => 'text',
) );
// Add other shared fields here.
}
-
This reply was modified 6 years, 9 months ago by
Justin Sternberg. Reason: fix typo
-
This reply was modified 6 years, 9 months ago by
Justin Sternberg. Reason: fix another typo