filters - Last updated date function

admin2025-06-05  9

I want to show the "last updated date" on the front of the post and page.
so I add the following code in the functions.php file.

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" style="text-align:right">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';
}
    $custom_content .= $content;
    return $custom_content;
}

add_filter( 'the_content', 'wpb_last_updated_date' );

It works,but I don`t want to show it on my home page.

How can I do ?

I want to show the "last updated date" on the front of the post and page.
so I add the following code in the functions.php file.

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" style="text-align:right">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';
}
    $custom_content .= $content;
    return $custom_content;
}

add_filter( 'the_content', 'wpb_last_updated_date' );

It works,but I don`t want to show it on my home page.

How can I do ?

Share Improve this question asked Dec 19, 2018 at 3:43 cindycindy 1059 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can check for your "home page" at the start of your function by using is_home() and/or is_front_page() and just return original content without date. See is_home vs is_front_page

function wpb_last_updated_date( $content ) {
  if ( is_home() || is_front_page() ) return $content; //homepage return content without date

  // your original function code

}

Alterantively you could embes your add_filter in an if statement

I finally solved the problem !!

function wpb_last_updated_date( $content ) {
if( is_front_page() || is_home() ) {
    return $content;
}
if( is_page(array(1017,927))){  
    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/1749082969a316214.html

最新回复(0)