Skip to content

Snippet: Custom Date Shortcode (can be used with Slider Revolution)

If you add the code below to your child theme's functions.php file it will add a new shortcode that you can use on your site to insert dates anywhere on the site. If using it in a post slider make sure to use the following format: [date id={{id}} format="F j, Y"] it's crucial to pass the id to the shortcode so that it knows what post the current slide is for.

function my_date_shortcode( $atts ) {
	$atts = shortcode_atts( array(
		'id'     => 'null',
		'format' => 'F j, Y',
	), $atts );
	$id = ! empty( $atts['id'] ) ? $atts['id'] : get_the_ID();
	$format = ! empty( $atts['format'] ) ? $atts['format'] : get_option( 'date_format' );
	return get_the_date( $format, $id );
add_shortcode( 'date', 'my_date_shortcode' );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top