在WordPress底部添加动态版权日期

在WordPress底部添加动态版权日期

一般网站都会在页脚加一个类似于Copyright 2013-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;
}

 

分享到 :