Support

Account

Home Forums General Issues Displaying shortcode of date_picker with format

Solving

Displaying shortcode of date_picker with format

  • Is there a way to use my date_picker custom field as a shortcode with a preferred format?

    For example, this already works and returns 01/12/15:
    [acf field="my_date"]

    But, it would be great if this could work and return Jan 12, 2015:
    [acf field="my_date" format="M j, Y"]

  • Hi @spinline

    This is not possible with the acf shortcode.

    May be you could consider implementing your own shortcode for implementing this. The code would be simple, It will look something like this:

    function acf_date_shortcode( $atts )
    {
    	// extract attributs
    	extract( shortcode_atts( array(
    		'field'			=> '',
    		'post_id'		=> false,
    		'format_value'	=> true,
    		'date_format' 	=> ''
    	), $atts ) );
    	
    	$acf_date = get_field( $field, $post_id, $format_value );
    	
    	$date = DateTime::createFromFormat('d/m/Y', $acf_date);
    	
    	$value = $date->format($date_format);
    	
    	return $value;
    }
    
    add_shortcode( 'acf_date', 'acf_date_shortcode' );

    and on the usage, it would be something like this: [acf_date field='date_field' date_format='M j, Y']

    For this code to work, the return date for the date field should be of this format: d/m/Y

    Hope this helps 🙂

  • Hi, is there any solution to get Translated Date?
    like “Donnerstag, 28. Mai 2020”

    i found no solution to translate with i18n.

    mby anyone can help me out

    thank you
    stoffl

  • I am looking for an options field, should I use ‘options’ as the post id? For some reason I get en error.

    Fatal error: Uncaught Error: Call to a member function format() on bool in /code/wp-content/themes/themename/functions.php:324
    Stack trace:
    #0 /code/wp-includes/shortcodes.php(356): acf_date_shortcode(Array, ”, ‘acf_date’)
    #1 [internal function]: do_shortcode_tag(Array)
    #2 /code/wp-includes/shortcodes.php(228): preg_replace_callback(‘/\\[(\\[?)(acf|wo…’, ‘do_shortcode_ta…’, ‘

  • Hi ! I also add an error generated with the above code, so I modified it using the date_i18n function so the date will be translated corresponding to your language. (it worked with wpml)

    Please note that for this to work, the format of your acf date field (for template) must be this type :
    20211010 Ymd

    As above, the syntax of the Shortcode is the same :
    [acf_date field='date_field' date_format='M j, Y']

    function acf_date_shortcode( $atts )
    {
    extract( shortcode_atts( array(
    		'field'			=> '',
    		'post_id'		=> false,
    		'format_value'	=> true,
    		'date_format' 	=> ''
    	), $atts ) );
    	
    	$acf_date = get_field( $field, $post_id, $format_value );
    	
    	$value = date_i18n( $date_format,strtotime($acf_date) );
    		return $value;
    }
    add_shortcode( 'acf_date', 'acf_date_shortcode' );
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.