Site logo

Archived topic

Trying to Show Post Update Date but Not Working

9 replies · Started by Tareq on April 7, 2021

Viewing posts 1–10 of 10

Hi,
I have been using this code to show last modified date instead of the published date in the blog posts.

/* Show Last Update Date on Post */

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on %4$s</time>';
    }
    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );
    return sprintf( '<span class="posted-on">%s</span> ',
        $time_string
    );
}, 10, 2 );

This is working just perfect in all my websites except the one mentioned in private info. Don't know what's causing the issue. I have other code in the child theme functions.php to add schema markup in header portion in specific posts. That's also not working.
I tried to put above mentioned code in code snippets plugin, but that also failed. Seems like the code is not executing. Don't know why!

Looking forward to a solution.

Best Regards

Also, I used css method mentioned in other support related to this topic also, but that doesn't work either.

Thanks for the reply. But sadly, this also is not working.

I have exported the design from one of my other website to this one in question. Could this be a reason for not executing the php?

That shouldn't be an issue.
Do you have any other functions on the site ?

I have a wp show post function to show related posts. This code before generate footer. This is present in other sites as well.

<div class="wpsp-related-posts  grid-container">
	<h3>Related Posts</h3>
<?php
if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0];
} else {
    $cat = get_category( get_query_var( 'cat' ) );
}

$cat_slug = $cat->slug;
$list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
?>
</div>

Apart from that, I didn't use any other code myself. I didn't use any new plugin in the site as well. All are present in another site, and last update date is showing perfectly there.

Hi Elvin,
Thanks for your tips. Can you please clarify what did you mean by 'comment out the filter'?

I have the code I mentioned in first comment working now after deactivating half of the active plugins. But thankfully, the issue didn't come back after I activated them all again. Miraculous or something, I don't know!

Hi Elvin,
Thanks for your tips. Can you please clarify what did you mean by ‘comment out the filter’?

"commenting out" a piece of code usually means temporarily disabling it. My bad. :)

I have the code I mentioned in first comment working now after deactivating half of the active plugins. But thankfully, the issue didn’t come back after I activated them all again. Miraculous or something, I don’t know!

Glad it's working for you now. :)

This archived topic is closed to new replies.