
一般网站都会在页脚底部添加类似Copyright ◎ 2013-2021版权信息,如果嫌每年都改这个日期麻烦,我们可以通过下面的方式添加一个动态版权日期。
效果展示
![图片[1]-WordPress网站添加动态版权日期代码-AA源码网 | 源码收藏](https://images.aaym.net/2023/10/20231026161831500-image.png!/format/webp/lossless/true)
教程如下
将下面代码添加到当前主函数模板functions.php
function mrxu_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
适当位置即可:
如果只是为了动态显示日期,下面的方法可能更实用些,只需一行代码:
也可以选择下序方法,通过JS方式调取年份:
<script>document.write(new Date().getFullYear());</script>
最初时间固定,后面直接读取服务器时间了。
本文结束END

© 版权声明
THE END
暂无评论内容