Home › Forums › Pro Support › Custom date format
Tagged: date format
- This topic has 7 replies, 2 voices, and was last updated 4 months, 2 weeks ago by
Tom.
-
AuthorPosts
-
September 30, 2020 at 10:09 am #20416
Eivind
ParticipantHey,
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!
October 1, 2020 at 8:01 pm #20523Tom
KeymasterHi 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 🙂
October 2, 2020 at 3:29 am #20556Eivind
ParticipantI 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 formatOctober 2, 2020 at 1:32 pm #20579Tom
KeymasterGot 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.October 2, 2020 at 11:52 pm #20593Eivind
ParticipantThanks, 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?
October 3, 2020 at 11:16 am #20611Tom
KeymasterCan you try the updated code above?
Let me know 🙂
October 4, 2020 at 4:28 am #20637Eivind
ParticipantWorked beautifully, thank you very much 😀
October 9, 2020 at 9:41 am #20862Tom
KeymasterGlad I could help! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.