无需插件自动在WordPress文末自动添加修改时间

老蒋在前面的文章中有记录到我们可以实现无插件在文章的头部可以添加WordPress文章更新日期,这样可以凸显当前文章是有修改更新过的,毕竟有些文章和产品是具有时效性的。有些朋友喜欢简单的更新日期插入在文章末尾,那如何实现呢?

无需插件自动在WordPress文末自动添加修改时间

// 文章末尾添加更新日期时间 https://www.itbulu.com/Update date-end-content.html
function itbulu_last_updated_date( $content ) {
	$u_time          = get_the_time( 'U' );
	$u_modified_time = get_the_modified_time( 'U' );
	$custom_content  = '';
	if ( $u_modified_time >= $u_time + 86400 ) {
		$updated_date   = get_the_modified_time( 'Y-m-d' );
		$custom_content .= '<div class="last-updated">文章最后更新于 ' . $updated_date . ' </div>';
    }
    $content .= $custom_content;

	return $content;
}
 
add_filter( 'the_content', 'itbulu_last_updated_date' );

这里,我们也可以根据需要添加一个 last-updated 的样式CSS。

.last-updated{color:red;}

我们可以根据需要选择以及自定义文本。

本文出处:老蒋部落 » 无需插件自动在WordPress文末自动添加修改时间 | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送