在制作主题的时候,我们不免要在底部加上版权,一般都是这样的显示的“Copyright XXX 1999 版权所有”,但是,过了一年,这里面的年份却不会自动改变怎么办?不慌,本文教你。在 functions.php 加上代码:
- /*
- WordPress实现动态获取并显示版权年份
- */
- function copyrightDate() {
- 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'
- ");
- if($copyright_dates) {
- $date = date('Y-m-d');
- $date = explode('-', $date);
- $copyright = "Copyright © " . $copyright_dates[0]->firstdate;
- if($copyright_dates[0]->firstdate != $date[0]) {
- $copyright .= '-' . $date[0];
- }
- echo $copyright;
- }
- }
那么我们在前端调用就可以填写:
- <?php copyrightDate();?>