WordPress技巧:动态显示网站创建时间的页脚版权信息
在制作WordPress主题的时候,通常需要在页脚输出建站时间,输出的内容如下所示:
因为主题是给用户使用的,并不确定用户搭建站点的时间,因此可以通过获取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 © ' . $first_date;
- if( $first_date != $last_date ) {
- $output .= ' - ' . $last_date;
- }
- ?>;
复制代码
|