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…’, ‘

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.