在独白标题中显示时间差(6小时前)

时间:2019-06-04 作者:Visible

感谢观看。我正在使用一个独白插件插件在幻灯片标题中显示帖子日期。我想将发布日期更改为人类差异或“小时前”格式。我正在转换get_the_date 在函数文件中使用以下内容

// Relative date & time
function wpse_relative_date() {
  return human_time_diff( get_the_time(\'U\'), current_time( \'timestamp\' ) ) . \' ago\';
}
add_filter( \'get_the_date\', \'wpse_relative_date\' );
插件是

function sol_soliloquy_fc_date_terms( $content, $post, $data ) {

    // Start Config
    $sliderSlugs = array(
        \'your-slider-slug-name\',
    );

    // Check slider ID is the one we want to amend
    if ( ! in_array( $data[\'config\'][\'slug\'], $sliderSlugs ) ) {
        return $content;
    }

    // Build date and taxonomy terms content
    $additional_content =       

    \'<div class="soliloquy-fc-date">\'
        . __( \'\', \'soliloquy-featured-content-date-terms\' ) . get_the_date( \'dS F Y\', $post->ID ) . \' 
    </div>\';


    // Return content
    return $content . $additional_content;

}
add_filter( \'soliloquy_fc_caption\', \'sol_soliloquy_fc_date_terms\', 1, 3 );
这会输出第一篇帖子创建的天数,其他所有帖子都是一样的,所以我猜它不会每次都返回帖子,因为它在循环之外。我是一名php新手,我猜这与不能使用$post->ID 内部get_the_date.

非常感谢您的帮助!

1 个回复
最合适的回答,由SO网友:nmr 整理而成

在筛选函数中,应将post ID传递给get_the_time() 函数,否则您将从当前帖子中获取日期。

function wpse_relative_date( $date, $format, $post ) {
   return human_time_diff( get_the_time(\'U\', $post), current_time( \'timestamp\' ) ) . \' ago\';
}
add_filter( \'get_the_date\', \'wpse_relative_date\', 15, 3 );
参考资料: