-
Hey guys i’ve followed this PHP short code and it worked on paragraph text https://generatepress.com/forums/topic/shortcode-for-current-month/
How can I make it work on heading and on my post title in WordPress? Also how can I make it for the code to always capitalize the first letter of the month word.
Example in [month] appear Janeiro instead of janeiro. (My site is in Portuguese so that’s why the language.)
This is my current shortcode.
add_shortcode(‘month’,function($atts){
ob_start();
$atts = shortcode_atts( array(
‘timezone’ => ”,
‘html_tag’ => ”,
‘class’ => ”,
‘format’ => ”,
‘id’ => ”
), $atts, ‘month’ );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_i18n($format, false, false).'</’.$html_tag.’>’;
return ob_get_clean();
}); -
Hi there,
Try only use
[month format="F Y"]
, remove[year]
. -
Thank you Ying! It worked! Any tips on Making the first word capitalize? Example is showing “janeiro 2025” I want to show “Janeiro 2025”
-
Alvind
Hi there,
Try replacing your current shortcode snippet with this one:
add_shortcode('month', function($atts) { ob_start(); $atts = shortcode_atts(array( 'timezone' => '', 'html_tag' => '', 'class' => '', 'format' => '', 'id' => '' ), $atts, 'month'); if ($atts['timezone']) { date_default_timezone_set($atts['timezone']); } $html_tag = empty($atts['html_tag']) ? 'span' : $atts['html_tag']; $id = empty($atts['id']) ? '' : sprintf('id="%s"', esc_attr($atts['id'])); $class = empty($atts['class']) ? '' : sprintf('class="%s"', esc_attr($atts['class'])); $format = empty($atts['format']) ? 'F' : $atts['format']; printf( '<%1$s %2$s %3$s>%4$s</%1$s>', esc_html($html_tag), $id, $class, esc_html(ucfirst(date_i18n($format, false, false))) ); return ob_get_clean(); });
-
thank you worked as well! You guys are amazing!
-
Alvind
You’re welcome!
- You must be logged in to reply to this topic.