我们都知道,一般网站都会在页脚添加个类似Copyright©2020-2030版权信息。但,如果每年都要手动修改这个日期的确显得有些麻烦,懒得朋友可以通过下面的方法添加一个动态的版权日期。
将下面代码添加到当前主题函数模板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" ); ?>
最初是时间固定,后面直接读取服务器时间!