0

我想完成什么?

  1. 使用 new_cmb2_box 添加新的选项页面 ->这工作正常

add_action( 'cmb2_admin_init', 'register_metabox' );

function register_metabox() {

    $cmb = new_cmb2_box( array(
        'id'           => 'testtesttest',
        'title'         => '_Ustawienia_Motywu_',
        'object_types' => array( 'options-page' ),
        'option_key'      => 'ustawienia_motywu',
        'parent_slug' => 'options-general.php'
    ) );

    }

  1. 为刚刚创建的选项页面(上面的第 1 点)添加一个元框,再次使用 new_cmb2_box ->这部分不起作用- 如何使其工作?:

add_action( 'cmb2_admin_init', 'register_metabox2' );

function register_metabox2() {

    $cmb2 = new_cmb2_box( array(
        'id'           => 'test2',
        'title'         => 'title',
        'object_types' => array( 'options-page' ),
        'parent_slug' => 'options-general.php?page=ustawienia_motywu',
    ) );

    // Set our CMB2 fields

    $cmb2->add_field( array(
        'name' => __( 'Test Text', 'myprefix' ),
        'desc' => __( 'field description (optional)', 'myprefix' ),
        'id'   => 'test_text',
        'type' => 'text',
        // 'default' => 'Default Text',
    ) );

    $cmb2->add_field( array(
        'name'    => __( 'Test Color Picker', 'myprefix' ),
        'desc'    => __( 'field description (optional)', 'myprefix' ),
        'id'      => 'test_colorpicker',
        'type'    => 'colorpicker',
        'default' => '#bada55',
    ) );

}

如何使#2工作?我的猜测可能是使用自定义 'show_on_cb' ...

4

1 回答 1

0

你应该把所有的功能都放在一个功能上:

add_action( 'cmb2_admin_init', 'register_metabox' );
function register_metabox() {

    $cmb = new_cmb2_box( array(
        'id'            => 'testtesttest',
        'title'         => '_Ustawienia_Motywu_',
        'object_types'  => array( 'options-page' ),
        'option_key'    => 'ustawienia_motywu',
        'parent_slug'   => 'options-general.php'
    ) );

    $cmb->add_field( array(
        'name' => __( 'Test Text', 'myprefix' ),
        'desc' => __( 'field description (optional)', 'myprefix' ),
        'id'   => 'test_text',
        'type' => 'text',
    ) );

    $cmb->add_field( array(
        'name'    => __( 'Test Color Picker', 'myprefix' ),
        'desc'    => __( 'field description (optional)', 'myprefix' ),
        'id'      => 'test_colorpicker',
        'type'    => 'colorpicker',
        'default' => '#bada55',
    ) );
}
于 2021-12-02T14:21:50.053 回答