• Resolved locofierro22

    (@locofierro22)


    Hi, I am trying to display a text_datetime_timestamp field on the frontend in a post with a different format, since it is a UNIX date.

    The metabox with the respective field is the following:

    add_action( 'cmb2_init', 'ccb_register_metabox' );
    /**
     * Hook in and add a demo metabox. Can only happen on the 'cmb2_init' hook.
     */
    function ccb_register_metabox() {
    
    	// Start with an underscore to hide fields from custom fields list.
    	$prefix = 'ccb_';
    
    	/**
    	 * Sample metabox to demonstrate the different conditions you can set.
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'            => $prefix . 'metabox',
    		'title'         => 'Valoración de la composición corporal',
    		'object_types'  => array( 'valoraciones' ), // Post type.
        	'show_on_cb' => 'be_taxonomy_show_on_filter', // function should return a bool value
        	'show_on_terms' => array(
    	    	   'tipo-valoraciones' => array( 'tvf-cc' ),
    	),
    	) );
    
    	/**
    	 * 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->add_field( array(
    		'name' => esc_html__( 'Fecha y hora', 'cmb2' ),
    		'id'   => 'fecha_hora',
    		'type' => 'text_datetime_timestamp',
    		'default_cb' => 'set_to_today',
    		'column' => array(
                'position' => 3,
                'name'     => 'Fecha de valoración',
            ),
    
    	) );

    I added the following function and shortcode:

    //Mostrar fecha y hora con formato en el frontend
    function mostrar_fecha_hora( $field ) {
    $text = get_post_meta( $field->object_id, 'fecha_hora', true );
    echo date('d/m/Y H:i A', $text ); // resultado 07/03/2016 08:40:00 AM
    }
    
    add_shortcode( 'fecha_hora_formateada', 'mostrar_fecha_hora' );

    And the short code in the post is:
    [fecha_hora_formateada]

    But I can’t get it to display on the frontend.

    What am I doing wrong?

    Thank you very much in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Two biggest things standing out to me are:

    1. Field objects aren’t passed to shortcodes so you’re going to need to use something like get_the_ID() or maybe grab the global $post object to get the post ID.
    2. Shortcodes need to return their value, not echo.

    Thread Starter locofierro22

    (@locofierro22)

    Is there an example on which I can base myself to do this?

    I appreciate any kind of help.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    function mostrar_fecha_hora() {
    	$text = get_post_meta( get_the_ID(), 'fecha_hora', true );
    	return date('d/m/Y H:i A', $text );
    }
    add_shortcode( 'fecha_hora_formateada', 'mostrar_fecha_hora' );
    

    Give this version a try.

    Thread Starter locofierro22

    (@locofierro22)

    Give this version a try.

    Excellent! Now it works.

    Thank you very much, Michael.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display fields in frontend’ is closed to new replies.