[year] and [month] inside the post/page

#629820
  • Resolved Autocentrum
    Rank Math free

    Hello!

    I want to ask you about this article https://rankmath.com/kb/add-current-year-in-post-title/.
    Nice work, I didn’t know it is possibe!

    But I am not sure if I can expect the same result wit the code Snippets plugin as I do not have a child theme on my WordPress instance?

    1. If yes, could you please provide me with the exact piece of code I should insert into the PHP tab (screenshot: https://recordit.co/Lu8NNA4IvS) and which scenario to choose (run everyhere or only on site front-end) of the Snippets plugin?

    2. Will be also grateful if you could provide me with the piece of code that will show not only [year] but also the current [month] on the front-end?

    3. And my last question is: Does it work only with the post/page titles? I need to insert the [month ][year] into my posts (inside paragraphs and h2, h3, h4…titles). Is there also a solution to this with RankMath?

    Will appreciate your response.

    Kind regards,

    Greg

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    1. In the Code Snippets plugin, please add the whole filter (after the first line) under the PHP tab and choose run everywhere.

    2. To also include the month, please update the filter to this one:

    function month_year_shortcode() {
    	$output = date("F Y");
    	return  $output;
    }
    add_shortcode('month_year', 'month_year_shortcode');
    
    // To allow Rank Math Frontend title to parse shortcode.
    add_filter(
    	'rank_math/frontend/title',
    	function ( $title ) {
    		return do_shortcode( $title );}
    );
    
    add_filter('wp_title', 'do_shortcode', 10);
    add_filter('the_title', 'do_shortcode', 10);

    Then, you should use this shortcode in the title [month_year].

    3. The shortcode also works on your content (headings, paragraphs, etc.) to output the month and year.

    Hope that helps.

    Thank you.

    Thank you very much!

    It works!

    …but only in English.

    Is it possible to force month to be diplayed in other language than “en”, e.g. “pl”.

    Hm…?

    Hello,

    In this case, please try replacing the first two lines of the code with these to switch it to PL language:

    function month_year_shortcode() {
            $fmt = new IntlDateFormatter('pl_PL', IntlDateFormatter::NONE, IntlDateFormatter::NONE);
            $fmt->setPattern('MMMM Y');
    	    $output = $fmt->format(new DateTime());

    Let us know how this goes.

    Thank you.

    Thank you very much!!!

    It is very good.

    However, in “pl” we have a variety of nouns and it doesn’t sound too good.

    Would you help me and add to this code what would allow the day number to be displayed as well?

    Kind regards,

    Greg

    Hello,

    You can replace the $fmt->setPattern('MMMM Y'); with $fmt->setPattern('dd MMMM Y'); to add the day in the shortcode output.

    Let us know how that goes. Looking forward to helping you.

    Thank you!

    But how about the shortcode. If I run [month_year] already what to do to run [day_month_year] as well?

    🙂

    Hello,

    Thank you for your patience.

    In this case, you may need to create another function to have them both. Please try this filter on your site instead:

    function month_year_shortcode() {
    	  $fmt = new IntlDateFormatter('pl_PL', IntlDateFormatter::NONE, IntlDateFormatter::NONE);
            $fmt->setPattern('MMMM Y');
    	    $output = $fmt->format(new DateTime());
    	return  $output;
    }
    add_shortcode('month_year', 'month_year_shortcode');
    
    function day_month_year_shortcode() {
    	  $fmt1 = new IntlDateFormatter('pl_PL', IntlDateFormatter::NONE, IntlDateFormatter::NONE);
            $fmt1->setPattern('dd MMMM Y');
    	    $output1 = $fmt1->format(new DateTime());
    	return  $output1;
    }
    add_shortcode('day_month_year', 'day_month_year_shortcode');
    
    // To allow Rank Math Frontend title to parse shortcode.
    add_filter(
    	'rank_math/frontend/title',
    	function ( $title ) {
    		return do_shortcode( $title );}
    );
    
    add_filter('wp_title', 'do_shortcode', 10);
    add_filter('the_title', 'do_shortcode', 10);

    Once done, you can insert the shortcodes [month_year] and [day_month_year] to your title and content.

    Hope that helps.

    I think you have already written so much on this issue that you deserve a big thank you.

    But I will also shyly ask what the full code would look like for all three options ie:

    [year]
    [month_year]
    [day_month_year]

    As you write this small code for these three options I suggest you insert it in the blog as a supplement to this article: https://rankmath.com/kb/add-current-year-in-post-title/.

    Surely your readers will appreciate how powerful your WordPress plugin is.

    Thank you!

    Hello,

    You’re welcome.

    You can add this function to the filter to capture the “year” only and use [year] as the shortcode:

    function year_shortcode() {
    	  $fmt2 = new IntlDateFormatter('pl_PL', IntlDateFormatter::NONE, IntlDateFormatter::NONE);
            $fmt2->setPattern('Y');
    	    $output2 = $fm2t->format(new DateTime());
    	return  $output2;
    }
    add_shortcode('year', 'year_shortcode');

    We’ll also pass this reference to our team to include the filter in the link you have shared.

    Looking forward to helping you.

    Thank you a lot.

    Problem solved!

    All the best!

    Greg

    Hello,
    
    You’re welcome.

    We are super happy that we have addressed your concerns. If you have any other questions in the future, know that we are here to help you.
    
    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
    
    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
    
    Thank you.

Viewing 11 replies - 1 through 11 (of 11 total)

The ticket ‘[year] and [month] inside the post/page’ is closed to new replies.