WordPress页脚输出Copyright时间函数

文 / @WordPress主题

在制作WordPress主题的时候,通常需要在页脚输出建站时间,输出的内容如下所示:

Copyright © 2016 - 2017

因为主题是给用户使用的,并不确定用户搭建站点的时间,因此可以通过获取Admin账号的注册时间来确定,代码如下:

function bzg_copyright_date() {
	global $wpdb;
	$date = $wpdb->get_var($wpdb->prepare( "SELECT user_registered FROM $wpdb->users WHERE ID = 1", "") );
	$output = '';
	if( ! empty( $date ) ) {
		$first_date = substr($date, 0, 4);
		$last_date = current_time('Y');
		$output = 'Copyright © ' . $first_date;
		if( $first_date != $last_date ) {
			$output .= ' - ' . $last_date;
		}
	}
	echo $output;
}

然后在footer.php模板中调用函数bzg_copyright_date()即可,调用方法如下:

<?php bzg_copyright_date(); ?>

如果仅仅是自己使用,站点的搭建时间是可以确定的,为了减少数据库查询,可以这样写:

<?php
	$first_date = '2017';
	$last_date = current_time('Y');
	$output = 'Copyright &copy; ' . $first_date;
	if( $first_date != $last_date ) {
		$output .= ' - ' . $last_date;
	}
?>
添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。