我正在通过函数打印上次修改的日期。带有以下代码的php
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\' );
问题是它也出现在页面上,但我只需要出现在帖子上。是否有
if
命令仅选择博客帖子?谢谢
最合适的回答,由SO网友:Jacob Peattie 整理而成
您可以使用get_post_type()
. 仅当值为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\' );