Display fields in frontend
-
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)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Display fields in frontend’ is closed to new replies.