Archived topic
Shortcode for Current Month
11 replies · Started by Praveen on March 14, 2021
Hi guys,
Is there a way to display the current month inside a post (with shortcode) without using any additional plugin?
Its not required in all articles, just in some.
Hi there,
There's no shortcode for this by default.
But you can reuse something I've written before:
add_shortcode('display_current_date',function($atts){
ob_start();
$atts = shortcode_atts( array(
'timezone' => '',
'html_tag' => '',
'class' => '',
'format' => '',
'id' => ''
), $atts, 'display_current_date' );
date_default_timezone_set($timezone);
if( $atts['timezone'] ){
date_default_timezone_set($atts['timezone']);
}
if( empty( $atts['html_tag'] ) ){
$html_tag = 'span';
} else{
$html_tag = $atts['html_tag'];
}
if( empty( $atts['id'] ) ){
$id = '';
} else{
$id = 'id="'.$atts['id'].'"';
}
if( empty( $atts['class'] ) ){
$class = '';
} else{
$class = 'class="'.$atts['class'].'"';
}
if( empty( $atts['format'] ) ){
$format = 'F';
} else{
$format = $atts['format'];
}
echo '<'.$html_tag.' '.$id.' '.$class.'>'.date($format).'</'.$html_tag.'>';
return ob_get_clean();
});
This PHP snippet allows you to use [display_current_date].
It has a couple of attributes but if you use the default [display_current_date] it will only display the month with <span> HTML tag.
Here are a few attributes to use.
Example: making the date display using <h2> element
[display_current_date html_tag="h2"]
Example: changing timezone to Asia/Manila.
[display_current_date timezone="Asia/Manila"]
List of supported timezones - [display_current_date html_tag="p" class="test" id="test-1" format="F j, Y, g:i a" timezone="Asia/Manila"]
Example: add class "test", add id "test-1" and use p HTML tag.
[display_current_date html_tag="p" class="test" id="test-1"]
Example: Changing date format to add day, year and timestamp incase you change your mind.
[display_current_date format="F j, Y, g:i a"]
Example: using all attributes.
[display_current_date html_tag="p" class="test" id="test-1" format="F j, Y, g:i a" timezone="Asia/Manila"]
perfect!
Is there a chance of this NOT working based on WordPress version updates?
Is there a chance of this NOT working based on WordPress version updates?
That's impossible to answer with 100% certainty. But it's most likely going to work on WordPress update unless they decide to stop using shortcodesadd_shortcode() and other WordPress functions. (very unlikely)
Love it! Thanks.
No problem. :D
Hi Elvin, awesome shortcode! Thank you very much. Is there a way to translate it to spanish (my wordpress default language)?
Thanks.
Hi there,
try changing this line of code:
echo '<'.$html_tag.' '.$id.' '.$class.'>'.date($format).'</'.$html_tag.'>';
to:
echo '<'.$html_tag.' '.$id.' '.$class.'>'.date_i18n($format, false, false).'</'.$html_tag.'>';
Thank you! I finally used this. That should be ok, right?
function display_date_1() {
return date_i18n('d/m/Y');
}
add_shortcode('date_1', 'display_date_1');
function display_date_2() {
return date_i18n('d-m-Y');
}
add_shortcode('date_2', 'display_date_2');
function display_date_3() {
return date_i18n('D d.m.Y');
}
add_shortcode('date_3', 'display_date_3');
function display_date_4() {
return "Hoy es ". date_i18n('l') .", " . date_i18n('d') . " de ". date_i18n('F'). " del año " .date_i18n('Y');
}
add_shortcode('date_4', 'display_date_4');
Looks good to me :)
Perfect, thanks!
You're welcome