WordPress 添加动态版权日期
一般网站都会在页脚添加个类似Copyright ©2000-2021版权信息,如果嫌每年都改这个日期麻烦,可以通过下面的方法添加一个动态版权日期。
将下面代码添加到当前主题函数模板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;
}
通过查询数据库中,最早与最后发表的文章时间,判断输出日期。
在合适的位置添加:
<?php echo zm_copyright(); ?>
原文来自:https://zmingcx.com/wordpress-dynamic-copyright-date.html
