Date Appearing Outside of the Main Body in Template

Posted in: Newspaper
Post count: 96

hi,

I want to be able to add a snippet that would appear above the body of my post when I edit a post and add content to an older post. This snippet would in effect say:

‘Last updated on April 19th 2019’. (or whatever date you edited it on).

I’m doing this because its not good practice to remove dates from a post or edit the original publish date. This is a good workaround.

However, the problem is when I add the code to my child functions.php it works okay, but, it appears on the homepage (it should not) it throws the date and words outside of the body to the left of the template body. See screenshot.

Here is the code:


function wpb_last_updated_date( $content ) {
$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
$updated_date = get_the_modified_time('F jS, Y');
$updated_time = get_the_modified_time('h:i a'); 
$custom_content .= '<p class="last-updated">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';  
} 
 
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

SCREENSHOT:

screenshot of issue

The date appears correctly everywhere else and as intended to be. It should NOT be appearing on the homepage. BTW it works outside of this template quite well, so I believe this is a conflict somewhere with this.

Here is where I found the original article that references this code:

How to Display the Last Updated Date of Your Posts in WordPress

Thank you

  • This topic was modified 7 years by VZ.
  • This topic was modified 7 years by VZ.
Post count: 20688

Hi,

So you want to display the modified date in the post page. You could use a cloud post template instead of code. If you use a cloud post template you can simply add the last modified date time format, in the template
http://prntscr.com/nil303
This would be the best way of displaying the modified date in the post page, and easiest.

Check out some single templates designed by us – https://cloud.tagdiv.com/#/load/Single You can also design your own, or modify the ones that you import with the cloud library plugin – https://forum.tagdiv.com/tagdiv-cloud-library-plugin/

Let me know if you have more questions.
Thanks

Viewing 2 posts - 1 through 2 (of 2 total)
The forum ‘Newspaper’ is closed to new topics and replies.