• I want to show the content updated date on my pages/posts, I’ve added this 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' );

    The thing is it works on my pages and some of the posts but not on all the posts! This code isn’t working on this post:

    Could anyone help me with this or is there something I have to change in the code? This post isn’t showing the updated date but the same code is working on other pages and posts!

    • This topic was modified 4 years, 7 months ago by James Huff. Reason: link moved to proper field

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to add $custom_content = ''; before the if() statement to avoid logging a bunch of PHP warnings about custom_content is undefined for posts that were not modified.

    As a test, add $custom_content = '<p>Not modified</p>'; instead. If this output shows up on the problem post, at least you know your filter callback is still being called. There is probably something about the timestamps returned that fails to meet the modified after 24 hours criteria. Output the difference in timestamp values to verify.

    Thread Starter faithrowan

    (@faithrowan)

    But the thing is the code is perfectly working on other posts!

    Moderator bcworkz

    (@bcworkz)

    Yeah, other than warnings when posts are not modified, it works for me. You’d need to basically debug the WP functions to trace back to what the problem might be. Temporarily error_log or var_dump various intermediate variables used in the process to identify which are not as expected. But first start with checking the values returned by the various get_*_time() functions. Something is not right somewhere. Work backwards into the involved function until the source of the problem is determined.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Updated date on the post not showing’ is closed to new replies.