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
2 Answers
Reset to default 0You 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' );