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

这样可以方便知道当前文章是有修改更新过的,毕竟有些文章和产品是具有时效性的。有些朋友喜欢简单的更新日期插入在文章末尾,那如何实现呢?

以下两种方法都需要single.php中输出了

方法一:插入文章末尾中:

  1. // 文章末尾添加更新日期时间
  2. function itbulu_last_updated_date( $content ) {
  3. 	$u_time          = get_the_time( 'U' );
  4. 	$u_modified_time = get_the_modified_time( 'U' );
  5. 	$custom_content  = '';
  6. 	if ( $u_modified_time >= $u_time + 86400 ) {
  7. 		$updated_date   = get_the_modified_time( 'Y-m-d' );
  8. 		$custom_content .= '<div class="last-updated">文章最后更新于 ' . $updated_date . ' </div>';
  9.     }
  10.     $content .= $custom_content;
  11.  
  12. 	return $content;
  13. }
  14.  
  15. add_filter( 'the_content', 'itbulu_last_updated_date' );

方法二:插入文章开头顶部:

  1. // 无插件最新更新时间
  2. function itbulu_post_update( $content ) {
  3. $u_time = get_the_time('U'); 
  4. $u_modified_time = get_the_modified_time('U'); 
  5. $custom_content = ''; 
  6. if ($u_modified_time >= $u_time + 86400) {
  7. $updated_date = get_the_modified_time('Y.m.d H:s'); //这里设置时间显示格式,可自由调整。
  8. $custom_content .= '<p class="itbulu-noplugin-update">本文最后更新于<code>'. $updated_date . '</code>,文章可能有时效性,若有错误或失效,请联系。</p>';  
  9. } 
  10.     $custom_content .= $content;
  11.     return $custom_content;
  12. }
  13. add_filter( 'the_content', 'itbulu_post_update' );
话题:

相关推荐