I am attempting to change the standard Wordpress date published to 'time ago' in the blog archive, but leave the date display inside the single posts as normal.
I have found the following function from https://mekshq.com/change-wordpress-date-format-to-time-ago/ :
add_filter( 'get_the_date', 'meks_convert_to_time_ago', 10, 1 ); //override date display
add_filter( 'the_date', 'meks_convert_to_time_ago', 10, 1 ); //override date display
add_filter( 'get_the_time', 'meks_convert_to_time_ago', 10, 1 ); //override time display
add_filter( 'the_time', 'meks_convert_to_time_ago', 10, 1 ); //override time display
/* Callback function for post time and date filter hooks */
function meks_convert_to_time_ago( $orig_time ) {
global $post;
$orig_time = strtotime( $post->post_date );
return human_time_diff( $orig_time, current_time( 'timestamp' ) ).' '.__( 'ago' );
}
Of course this function applies the 'time ago' change site-wide, I would like to understand how I can have this apply only in the blog archive list view, not the single posts.
Thankyou
CodePudding user response:
This is checking if it is the blog page so you would need to just if you have it on a different page.
add_filter('get_the_date', 'meks_convert_to_time_ago', 10, 1); //override date display
add_filter('the_date', 'meks_convert_to_time_ago', 10, 1); //override date display
add_filter('get_the_time', 'meks_convert_to_time_ago', 10, 1); //override time display
add_filter('the_time', 'meks_convert_to_time_ago', 10, 1); //override time display
/* Callback function for post time and date filter hooks */
function meks_convert_to_time_ago($orig_time) {
if (is_home() ) {
global $post;
$orig_time = strtotime($post->post_date);
return human_time_diff($orig_time, current_time('timestamp')) . ' ' . __('ago');
} else {
return $orig_time;
}
}
