PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] 'Time ago' display in Wordpress blog archive

 February 08, 2022     php, wordpress     No comments   

Issue

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


Solution

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;
    }
}


Answered By - Mark Truitt
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing