服务公告

服务公告 > 网络资讯 > WordPress更新文章 显示最近更新时间

WordPress更新文章 显示最近更新时间

发布时间:2022-11-28 17:12
 

      有些资源站文章具有时效性,文章添加一个最近更新时间,提示文章最近更新的时间。
没有修改或当天修改过的文章不显示,其他的时间修改更新文章就显示提示文章最近更新时间。

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('Y年m月d日 G:i:s');
		$custom_content .= '<p>本文最后更新于:'. $updated_date . '</p>';
	} 

    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

     以上代码需要添加到 functions.php

      上面那个没有CSS美化 单纯文本 需要美化请自行添加样式
      子比主题可以使用下面的代码进行美化

<?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('Y年m月d日 G:i:s');
		$custom_content .= '<div class="wp-block-zibllblock-alert alert-dismissible fade in"><div class="alert jb-blue">最近更新:'. $updated_date . '</div></div>';
	} 

    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

     子比主题为了防止更新后失效 请在目录内新建func.php 文件 插入上方代码即可