$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Print last modified date only on posts|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Print last modified date only on posts

matteradmin8PV0评论

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' );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far