- This topic has 11 replies, 4 voices, and was last updated 4 years, 2 months ago by
David.
-
AuthorPosts
-
March 14, 2021 at 11:37 pm #1695318
Praveen
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.
March 15, 2021 at 12:09 am #1695345Elvin
StaffCustomer SupportHi 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"]
March 15, 2021 at 2:55 am #1695565Praveen
perfect!
Is there a chance of this NOT working based on WordPress version updates?
March 15, 2021 at 3:50 am #1695628Elvin
StaffCustomer SupportIs 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)
March 15, 2021 at 4:10 am #1695640Praveen
Love it! Thanks.
March 15, 2021 at 2:54 pm #1696531Elvin
StaffCustomer SupportNo problem. 😀
July 18, 2021 at 10:05 am #1860130Tim
Hi Elvin, awesome shortcode! Thank you very much. Is there a way to translate it to spanish (my wordpress default language)?
Thanks.
July 18, 2021 at 3:40 pm #1860343David
StaffCustomer SupportHi 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.'>';
July 20, 2021 at 3:03 am #1862391Tim
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');
July 20, 2021 at 3:06 am #1862394David
StaffCustomer SupportLooks good to me 🙂
July 20, 2021 at 3:07 am #1862398Tim
Perfect, thanks!
July 20, 2021 at 4:14 am #1862487David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.