WordPress主题文章最近更新日期的显示办法有多种,可以用插件实现,也可以手动添加代码实现。而这几种方法也同样适合所有Wordpress主题。本文中老魏以GeneratePress主题文章最新修改日期为例,讲解为什么要显示文章最近更新时间和如何在Wordpress主题显示最近更新日期。
WordPress主题的文章存在发布日期和最新更新日期两个参数(这里面的文章包括:文章/页面/自定义等文章形式(post type),默认用文章代指所有的形式)。基于搜索引擎的搜索结果会展示时间,从访客角度出发考虑我们可以把文章发布时间改为最近更新时间。Google和国内搜索引擎都是这么显示的。
既然有文章发布时间和最近更新时间的区别。那么能够显示为最近更新时间会有以下几个好处:
- 为了保证访客获得最近更新的内容;
- 让搜索引擎显示SERP中的最后修改日期;
- 增加博客点击率(CTR);
SERP:search engin result page搜索结果页面,如下图所示;

google和国内搜索引擎的搜索结果显示文章发布时间
WordPress把文章发布日期和修改日期存储在数据库中,如果你不在WordPress中使用最近更新日期的功能,那么不管你更新文章多少次,都只显示文章发布日期而不是更新日期。为了让网站用户能够获得内容的最新版本,同时在Google或其他搜索引擎的SERP中显示的是最近更新日期,你需要在博客中添加更新日期的功能,有助于提高网站的点击率(CTR)。
国外建站主题及插件选择2核4G内存3M带宽能流畅运行。国内服务器:阿里云、腾讯云(都有代金券,付款时抵用省钱), 不知道如何选择地域、配置请在页面右侧+老魏的联系方式,我们一起研究。
外贸网站、B2C/C2C等建议在国外注册域名。打开namesilo官网,在搜索框里输入要注册的域名,选择好之后注册,付费之前记得使用优惠码省钱。优惠码点击国外域名商Namesilo域名注册教程获取。
外贸网站建议选择Hostinger虚拟主机,价格便宜,性价比极高!Hostinger专题
SiteGround虚拟主机价格适中服务好!SiteGround专题
使用WP Last Modified Info插件显示最近更新日期
如果你不懂如何手动添加代码及后期维护,那么使用插件显然是见效最快,最容易的。
WP Last Modified Info插件就是这么一款插件,感兴趣的话建议去看看。用这一个插件就够了,且对所有 WordPress主题都适用。
如果不想使用这个插件,那么可以试试下面的方法。
使用Code Snippets插件显示最近更新日期
Code Snippets插件是WordPress管理自定义代码插件,适合于经常往 functions.php添加代码的同学使用。如果你之前就已经在使用这款插件,那么也不要用上面那个插件了,一并用Code Snippets插件就好了。
为什么要用这个插件?如果你的WordPress网站运行时间久了就会发现总是有会这样那样的需求,需要添加到 functions.php中,所以如果能有这么一款插件就可以方便添加、管理很多个代码了。
我们在 WordPress仪表盘>插件>安装插件中搜索Code Snippets,安装并启用。
在Code Snippets插件中点击 add new,给代码片段起个名字,在 Code中复制并粘贴以下代码,Description可以输入注释,以便后期维护是知道这端代码的用途。点击 save and activate按钮。
显示最近更新日期的三种代码
在Code Snippets插件插入以下三种代码,供我们在不同情况下用来显示最近更新日期。
老魏提示:
1、这三种代码只适用于GeneratePress主题(懂代码可以自行修改),如果你使用别的主题也可以用上面提到的WP Last Modified Info插件设置;
2、把代码中的 published on 改成 发布于,Last Updated on 改成 最近更新于,就可以用于中文WordPress网站了。老魏测试通过;
3、改成最新更新日期、最近修改日期、最近更新日期、最新修改日期,巴拉巴拉按照个人喜好选择吧;
一、每次都显示“ Last Updated on ”,也就是不管文章是否修改/更新过,文章中都显示Last Updated on。
add_filter( 'generate_post_date_output', function( $output, $time_string ) { $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>'; if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) { $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %4$s</time>'; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date() ) ); return sprintf( '<span class="posted-on">%s</span> ', $time_string ); }, 10, 2 );
二、以下代码段会比较文章发布日期和更新日期,只有在有日期差别时它才应显示已更新。如果文章是最新的将显示“Published on”,而当文章更新时将显示“Last Updated on”。
add_filter( 'generate_post_date_output', function( $output, $time_string ) { $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time>'; if ( get_the_date() !== get_the_modified_date() ) { $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %4$s</time>'; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date() ) ); return sprintf( '<span class="posted-on">%s</span> ', $time_string ); }, 10, 2 );
三、同时显示显示发布日期和最后更新日期。如果要在 GeneratePress主题中同时显示“发布日期”和“最近更新日期”,要使用以下代码:
add_filter( 'generate_post_date_output', function( $output, $time_string ) { $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>'; if ( get_the_date() !== get_the_modified_date() ) { $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time> | <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %4$s</time>'; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date() ) ); return sprintf( '<span class="posted-on">%s</span> ', $time_string ); }, 10, 2 );
使用插件或添加代码在Wordpress主题或GeneratePress主题中显示最近更新日期后,然后可以提交搜索引擎更新后查看搜索结果中的时间显示。
老魏点评
WordPress文章最近更新日期的设置好处多多,既能提高搜索引擎重视,又能吸引用户点击。而我们要做的仅仅是根据个人情况使用插件或添加一段代码而已。文中的代码适合GeneratePress主题文章最新修改日期功能使用,如果你懂代码也可以修改成适合你的主题使用。