WordPress Development Asked by wittich on February 10, 2021
The template I’m using outputs the date of a post using:
echo get_the_date( get_option( 'date_time' ) );
I was wondering why this output is now (after upgrade to WP 5.5) not working anymore?
A fast fix is using the option date_format
:
echo get_the_date( get_option( 'date_format' ) );
I looked into WordPress Developer https://developer.wordpress.org/reference/functions/get_the_date/#source looks like it suppose also to work when the option ‘date_time’ is empty:
function get_the_date( $format = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( '' === $format ) { // <-- check for empty value
$the_date = get_post_time( get_option( 'date_format' ), false, $post, true );
} else {
$the_date = get_post_time( $format, false, $post, true );
}
return apply_filters( 'get_the_date', $the_date, $format, $post );
}
Why does the output of get_the_date(get_option('date_time'))
in the template doesnt work with WordPress 5.5?
Okay I found the issue, it's like I thought. The problem is that get_option( 'date_time' )
return just false
.
The change happened in the funtion get_the_date()
.
In Wordpress 5.4 is looked like that in the general-template.php:
function get_the_date( $format = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( '' == $format ) {
$the_date = get_post_time( get_option( 'date_format' ), false, $post, true );
} else {
$the_date = get_post_time( $format, false, $post, true );
}
return apply_filters( 'get_the_date', $the_date, $format, $post );
}
Since Wordpress 5.5 the general-template.php looks like that:
function get_the_date( $format = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( '' === $format ) {
$the_date = get_post_time( get_option( 'date_format' ), false, $post, true );
} else {
$the_date = get_post_time( $format, false, $post, true );
}
return apply_filters( 'get_the_date', $the_date, $format, $post );
}
The difference is just in the type true comparison operators
// Wordpress 5.4
if ( '' == $format )
// vs
// Wordpress 5.5
if ( '' === $format )
I think the correct solution would be to check for and empty or false
$format
. Something like that:
if ( '' === $format || false === $format )
Answered by wittich on February 10, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP