WordPress 添加动态版权日期,可以为网站添加一个自动化的版权日期
WordPress 添加动态版权日期,可以为网站添加一个自动化的版权日期。
一帮来说,很多网站都会在页脚添加类似 Copyright © 2018-2021 版权信息,如果你嫌每天都要改这个日期,可以通过下面的方法添加一个动态版权日期。
![WordPress 添加动态版权日期](https://obs.sg7z.com/pic/2022/08/20220805081955737.webp)
将下面的代码添加到当前主题函数模板 functions.php 中:
function zm_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("SELECT YEAR(min(post_date_gmt)) AS firstdate, YEAR(max(post_date_gmt)) AS lastdate FROM $wpdb->posts WHERE post_status = 'publish'");
$output = '';
if( $copyright_dates ) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if( $copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate ) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
将下面调用代码添加到页脚模板 footer.php 适当位置即可。
<?php echo zm_copyright(); ?>
如果只是为了动态显示日期,下面的方法更实用,只需要一行代码:
Copyright © 2000 - <?php echo date( "Y" ); ?>
最初时间固定,后面直接读服务器时间。