We're merging with GenerateBlocks! Learn more here.

[Resolved] Custom date format

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Custom date format

Tagged: 

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #20416
    Eivind
    Participant

    Hey,

    I have a multilingual website where I’m also displaying WPSP for different languages. The issue is that the date format displayed is equal to the default WP format (as specified in Settings -> General) for all languages. I need to use a different date format for certain languages. Because each list for each language has a unique ID, I think this can be achieved by adding a function for specific list IDs.

    For example, I need the date format to be “j. F Y” (30. september 2020) instead of “F j, Y” (September 30, 2020) for specific lists.

    Is it possible you could provide a code snippet to solve this using a function?

    Thanks!

    #20523
    Tom
    Keymaster

    Hi there,

    There is a filter in WPSP, but it can become a lot of code if you’re doing it more than once. This has to only happen for specific lists? You want other lists to have different formats even when using another language? If not, this filter is likely better: https://developer.wordpress.org/reference/functions/get_the_date/

    Let me know 🙂

    #20556
    Eivind
    Participant

    I didn’t fully understand your answer so let me try to explain again:

    I have 3 lists today in English. I have translated these 3 lists using WPML, and they have a different list ID than the original English. All lists are now showing the date format as defined in WP settings, but the date format for the translated lists needs to be different as I said above.

    See image here: https://ibb.co/7WQRTFL

    ID 10348, 10345, 10324: correct date format
    ID 10464, 10463, 10436: need to change the date format

    #20579
    Tom
    Keymaster

    Got it, so we can use the above filter.

    For example:

    add_filter( 'get_the_date', function( $the_date ) {
        if ( 'es_ES' === get_locale() ) {
            $the_date = get_post_time( 'j. F Y', false, get_the_ID(), true );
        }
    
        return $the_date;
    } );

    You just need to update the es_ES to the language you’re targeting.

    #20593
    Eivind
    Participant

    Thanks, that did fix the date format, but now the month is no longer translated. The month is now showing the English term instead of the correct local term which it did before applying the function.

    See here: https://ibb.co/82hJDCp

    Is it possible to just change the format while keeping the local translation?

    #20611
    Tom
    Keymaster

    Can you try the updated code above?

    Let me know 🙂

    #20637
    Eivind
    Participant

    Worked beautifully, thank you very much 😀

    #20862
    Tom
    Keymaster

    Glad I could help! 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.