Bonjour,
j'ai un group metabox qui ressemble a ca :
add_action( 'cmb2_init', 'domaine_register_repeatable_group_field_metabox' );
function domaine_register_repeatable_group_field_metabox() {
$prefix = 'klkchose';
/**
* Repeatable Field Groups
*/
$cmb_group = new_cmb2_box( array(
'id' => $prefix.'cmb_demo',
'title' => esc_html__( 'Repeating Field Group', 'cmb2' ),
'object_types' => array( 'page', ),
) );
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_group->add_field( array(
'id' => $prefix . 'demo',
'type' => 'group',
'description' => esc_html__( 'Generates reusable form entries', 'cmb2' ),
'options' => array(
'group_title' => esc_html__( 'Dossier de compétence {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => esc_html__( 'Add Another Entry', 'cmb2' ),
'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ),
'sortable' => true, // beta
// 'closed' => true, // true to have the groups closed by default
),
) );
/**
* Group fields works the same, except ids only need
* to be unique to the group. Prefix is not needed.
*
* The parent field's id needs to be passed as the first argument.
*/
$cmb_group->add_group_field( $group_field_id, array(
'name' => esc_html__( 'Entry Title', 'cmb2' ),
'id' => 'title',
'type' => 'text',
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => esc_html__( 'Votre Métier', 'cmb2' ),
'id' => 'title2',
'type' => 'text',
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb_group->add_group_field( $group_field_id,array(
'name' => esc_html__( 'Selectionner votre domaine', 'cmb2' ),
'desc' => esc_html__( 'field description (required)', 'cmb2' ),
'id' => $prefix . 'taxonomy_select',
'type' => 'taxonomy_select',
'taxonomy' => 'job_listing_category', // Taxonomy Slug
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => esc_html__( 'Description', 'cmb2' ),
'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ),
'id' => 'description',
'type' => 'textarea_small',
) );
}
et j'aimerai l'afficher dans une template page que j'ai crée nomée test.php et qui est dans le meme dossier que functions.php
je sais qu'il faut que j'ajoute ce bout de code :
https://github.com/CMB2/CMB2/issues/888.
mais ça ne marche pas.
à l'aide svp !!
j'ai ajouté le code depuis le lien et ca donne ca :
add_action( 'cmb2_init', 'domaine_register_repeatable_group_field_metabox' );
function domaine_register_repeatable_group_field_metabox() {
global $post;
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
$prefix = 'klkchose';
/**
* Repeatable Field Groups
*/
if($pageTemplate == 'test.php' ){
$cmb_group = new_cmb2_box( array(
'id' => $prefix.'cmb_demo',
'title' => esc_html__( 'Repeating Field Group', 'cmb2' ),
'object_types' => array( 'page', ),
) );
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_group->add_field( array(
'id' => $prefix . 'demo',
'type' => 'group',
'description' => esc_html__( 'Generates reusable form entries', 'cmb2' ),
'options' => array(
'group_title' => esc_html__( 'Dossier de compétence {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => esc_html__( 'Add Another Entry', 'cmb2' ),
'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ),
'sortable' => true, // beta
// 'closed' => true, // true to have the groups closed by default
),
) );
/**
* Group fields works the same, except ids only need
* to be unique to the group. Prefix is not needed.
*
* The parent field's id needs to be passed as the first argument.
*/
$cmb_group->add_group_field( $group_field_id, array(
'name' => esc_html__( 'Entry Title', 'cmb2' ),
'id' => 'title',
'type' => 'text',
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => esc_html__( 'Votre Métier', 'cmb2' ),
'id' => 'title2',
'type' => 'text',
// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb_group->add_group_field( $group_field_id,array(
'name' => esc_html__( 'Selectionner votre domaine', 'cmb2' ),
'desc' => esc_html__( 'field description (required)', 'cmb2' ),
'id' => $prefix . 'taxonomy_select',
'type' => 'taxonomy_select',
'taxonomy' => 'job_listing_category', // Taxonomy Slug
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => esc_html__( 'Description', 'cmb2' ),
'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ),
'id' => 'description',
'type' => 'textarea_small',
) );
} }
mais malheureusement ca ne m'affiche plus rien.
Salut,
Tu peux le faire directement dans la déclaration de ton groupe, en une ligne.
$cmb_group = new_cmb2_box( array(
'id' => $prefix.'cmb_demo',
'title' => esc_html__( 'Repeating Field Group', 'cmb2' ),
'object_types' => array( 'page', ),
'show_on' => array( 'key' => 'page-template', 'value' => 'test.php' ),
) );
Source : La doc' officielle