Auto Add Dynamic Year in WordPress Post Titles

#145683
  • Resolved Ioanna Karelia
    Rank Math free

    Hi,

    I would like to add Auto Add Dynamic Year in WordPress Post Titles.

    I am entering the following code in functions.php but it is not working.

    //* Shortcode to display the current year in WordPress
    //* shortcode: [year]

    add_shortcode( ‘year’ , ‘current_year’ );
    function current_year() {
    $year = date(“Y”);
    return “$year”;
    }

    //* Activate shortcode function in Post Title
    add_filter( ‘the_title’, ‘do_shortcode’ ); // activate shortcode in WP Title

    function my_custom_RM_title_replace_vars( $title, $id = null ) {

    return RankMath\Helper::replace_vars( $title );
    }
    add_filter( ‘the_title’, ‘my_custom_RM_title_replace_vars’, 10, 2 );

    What code do I need to use instead?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Roel John
    Rank Math business

    Hello,

    Thank you for contacting Rank Math.

    You can try the code below.

    
    function filter_the_title( $title, $id ) { 
        // make filter magic happen here... 
        $year = get_post_time( 'Y' );
        return ''.$title.' '.$year.''; 
    }; 
    
    add_filter( 'the_title', 'filter_the_title', 10, 2 ); 
    

    Please let us know if this helps you so we can assist you further on this issue.

    Thank you.

    Hi Roel,

    Your code works but it shows 2020 instead of the current year that is 2021. What alteration do I need to make? Thanks.

    Just changed the code to the following and it now shows 20201 in the post titles:

    //* Shortcode to display the current year in WordPress
    //* shortcode: [year]

    function filter_the_title( $title, $id ) {
    // make filter magic happen here…
    $year = date(“Y”);
    return ”.$title.’ ‘.$year.”;
    };

    add_filter( ‘the_title’, ‘filter_the_title’, 10, 2 );

    There is a problem now. The titles of of my site’s header menu also include 2021 now.

    Is there a way to limit what I am trying to do to WP Posts only?

    Hello,

    You can limit the function to work only posts by adding a conditional statement:
    if is_single() before executing.

    Hope this helps you.

    Hi,

    So the code would be as follows?

    if is_single()
    function filter_the_title( $title, $id ) {
    // make filter magic happen here…
    $year = get_post_time( ‘Y’ );
    return ”.$title.’ ‘.$year.”;
    };

    add_filter( ‘the_title’, ‘filter_the_title’, 10, 2 );

    Hi,

    Try the following:

    
    function filter_the_title( $title, $id ) {
    if is_single(){
    // make filter magic happen here…
    $year = get_post_time( 'Y' );
    return ".$title.' '.$year.";
    }
    return ".$title.' '.$year.";
    };
    
    add_filter( 'the_title', 'filter_the_title', 10, 2 );
    

    Hope this helps you.

    Thank you Michael.

    Febby
    Rank Math free

    Hello,

    We are super happy that this resolved your issue. 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 9 replies - 1 through 9 (of 9 total)

The ticket ‘Auto Add Dynamic Year in WordPress Post Titles’ is closed to new replies.