php - Print last modified date only on posts

admin2025-06-03  3

I'm printing the last modified date via functions.php with the following 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 problem is that it appears on pages too, but I need to appear only on posts. Is there an if command to choose only blog posts? Thanks

I'm printing the last modified date via functions.php with the following 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 problem is that it appears on pages too, but I need to appear only on posts. Is there an if command to choose only blog posts? Thanks

Share Improve this question edited Feb 15, 2019 at 9:07 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 15, 2019 at 9:00 NicoCaldoNicoCaldo 1472 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can check the post type with get_post_type(). You only want to display the content if the value is post:

function wpb_last_updated_date( $content ) {
    $u_time          = get_the_time( 'U' ); 
    $u_modified_time = get_the_modified_time( 'U' ); 

    if ( get_post_type() === 'post' ) {
        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' );

All you have to do is to check, if current post is a post and not a page:

function wpb_last_updated_date( $content ) {
    if ( 'post' != get_post_type() ) return $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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748904562a314689.html

最新回复(0)