Condicional metabox
-
Hello.
I am trying to add a metabox depending on the choice of another metabox.In the first metabox I choose the category of the custom post and depending on the choice I need to specify metabox.
In the following code it should show the metabox “skulpt_metabox” but I can’t get it to work.
Thank you very much in advance.
/** * Agregar un metabox valoración funcional para elegir el tipo de valoracion **/ add_action( 'cmb2_init', 'tv_register_metabox' ); /** * Hook in and add a demo metabox. Can only happen on the 'cmb2_init' hook. */ function tv_register_metabox() { // Start with an underscore to hide fields from custom fields list. $prefix = 'tv_'; /** * Sample metabox to demonstrate the different conditions you can set. */ $cmb_demo = new_cmb2_box( array( 'id' => $prefix . 'metabox', 'title' => 'Tipo de valoración funcional', 'object_types' => array( 'valoracion-funcional' ), // Post type. ) ); /** * 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_demo->add_field( array( 'name' => esc_html__( 'Tipo de valoración', 'cmb2' ), 'desc' => esc_html__( 'Elegir una opción', 'cmb2' ), 'id' => 'taxonomy_radio', 'type' => 'taxonomy_select_hierarchical', // Or <code>taxonomy_radio_inline</code>/<code>taxonomy_radio_hierarchical</code> 'taxonomy' => 'tipo-valoracion-funcional', // Taxonomy Slug // 'inline' => true, // Toggles display to inline // Optionally override the args sent to the WordPress get_terms function. 'query_args' => array( // 'orderby' => 'slug', // 'hide_empty' => true, ), ) ); } /** * Agregar un metabox valoración de la composicion corporal **/ add_action( 'cmb2_init', 'skulpt_register_metabox' ); /** * Hook in and add a demo metabox. Can only happen on the 'cmb2_init' hook. */ function skulpt_register_metabox() { // Start with an underscore to hide fields from custom fields list. $prefix = 'skulpt_'; /** * Sample metabox to demonstrate the different conditions you can set. */ $cmb_demo = new_cmb2_box( array( 'id' => $prefix . 'metabox', 'title' => 'Valoración de la composición corporal', 'object_types' => array( 'valoracion-funcional' ), // Post type. 'show_on_cb' => 'cmb_only_show_for_cc_b', // function should return a bool value ) ); /** * 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_demo->add_field( array( 'name' => esc_html__( 'Fecha y hora', 'cmb2' ), 'id' => 'fecha_hora', 'type' => 'text_datetime_timestamp', 'attributes' => array( // CMB2 checks for datepicker override data here: 'data-datepicker' => json_encode( array( 'yearRange' => '-100:+0', 'closeText' => 'Cerrar', 'prevText' => '< Ant', 'nextText' => 'Sig >', 'currentText' => 'Hoy', 'monthNames' => ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'], 'monthNamesShort' => ['Ene','Feb','Mar','Abr', 'May','Jun','Jul','Ago','Sep', 'Oct','Nov','Dic'], 'dayNames' => ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'], 'dayNamesShort' => ['Dom','Lun','Mar','Mié','Juv','Vie','Sáb'], 'dayNamesMin' => ['Do','Lu','Ma','Mi','Ju','Vi','Sá'], 'firstDay' => 1, //Hace que la semana comience en Lunes //'minDate' => 0, //Hace que no se puedan seleccionar fechas pasadas 'dateFormat' => "dd/mm/yy", //Establece el formato de fecha ) ), ), ) ); $cmb_demo->add_field( array( 'name' => esc_html__( 'Calidad Muscular', 'cmb2' ), 'id' => 'calidad_muscular', 'type' => 'text', // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types) ) ); $cmb_demo->add_field( array( 'name' => esc_html__( 'Porcentaje Masa Grasa', 'cmb2' ), 'id' => 'masa_grasa', 'type' => 'text', // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types) ) ); $cmb_demo->add_field( array( 'name' => esc_html__( 'Porcentaje Masa Magra', 'cmb2' ), 'id' => 'masa_magra', 'type' => 'text', // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types) ) ); $cmb_demo->add_field( array( 'name' => esc_html__( 'Peso Corporal Total', 'cmb2' ), 'id' => 'peso_corporal_total', 'type' => 'text', // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types) ) ); } function cmb_only_show_for_cc_b ( $cmb ) { $status = get_post_meta( $cmb->object_id(), 'taxonomy_radio', 1 ); // Only show if status is 'skulpt-basico' return 'skulpt-basico' === $status; }
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Condicional metabox’ is closed to new replies.