• Resolved locofierro22

    (@locofierro22)


    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)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I think part of the biggest issue here is that the values aren’t set yet, at least if you’re wanting a metabox to show as you’re filling things in, but have not yet saved and refreshed the page yet.

    That said, I know https://github.com/jcchavezs/cmb2-conditionals still works overall from my experiences, so worth checking out.

    Thread Starter locofierro22

    (@locofierro22)

    I think part of the biggest issue here is that the values aren’t set yet, at least if you’re wanting a metabox to show as you’re filling things in, but have not yet saved and refreshed the page yet.

    Once I choose a category, in the first metabox, I put update and it still does not appear. Also I leave and re-enter to the edition of the custom post and neither. The category, on the other hand, is saved.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks like your second metabox listed is dependent on the first metabox listed, but the first metabox is setting your taxonomy terms, not post meta.

    So instead of doing get_post_meta fetches, you need to grab the terms on the object, from the appropriate taxonomy.

    Thread Starter locofierro22

    (@locofierro22)

    Yes, the example I relied on did not apply to me.

    I solved it with the following link:

    Problem solved.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Glad to hear. Let us know if you need anything else.

    Thread Starter locofierro22

    (@locofierro22)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Condicional metabox’ is closed to new replies.