有些注重网站SEO的朋友应该会发现,搜索引擎对于时间戳还是比较敏感和喜欢的。于是,我们会在编辑旧文章的时候打上文章更新的时间戳显示最后的更新时间。当然我们可以用Last Updated Shortcode插件实现,这里我们可以直接用代码实现。
//简单代码实现WordPress文章更新时间戳 调用最后更新时间 https://www.zhujipingjia.com/wp-updated-time.html function wp_last_updated_time( $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('F jS, Y'); $updated_time = get_the_modified_time('h:i a'); $custom_content .= '<p class="wp_last-updated">本文最后更新时间: '. $updated_date . ' at '. $updated_time .'</p>'; } $custom_content .= $content; return $custom_content; } add_filter( 'the_content', 'wp_last_updated_time' );
这里,我们可以将代码添加到 Functions.php 中,还需要用一个CSS样式。
.wp_last-updated{padding:15px 15px;background-color:hsla(0,0%,100%,.1);border-radius:5px;border:1px solid;font-size:14px;text-align:left}
当然,上面的代码是实现日期的,我们还可以精确到秒的。
//简单代码实现WordPress文章更新时间戳 调用最后更新时间 https://www.zhujipingjia.com/wp-updated-time.html function wp_last_updated_time( $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 H:s'); //这里设置时间显示格式,可自由调整。 $custom_content .= '<p class="wp_last-updated">本文最后更新于<code>'. $updated_date . '</code>。</p>'; } $custom_content .= $content; return $custom_content; } add_filter( 'the_content', 'wp_last_updated_time' );
两者选其一即可。
评论