WordPress实现动态获取并显示版权年份

在制作主题的时候,我们不免要在底部加上版权,一般都是这样的显示的“Copyright © XXX 1999 版权所有”,但是,过了一年,这里面的年份却不会自动改变怎么办?不慌,本文教你。在 functions.php 加上代码:

  1. /*
  2. wordpress实现动态获取并显示版权年份
  3. */
  4. function copyrightDate() {
  5. global $wpdb;
  6. $copyright_dates = $wpdb->get_results("
  7. SELECT
  8. YEAR(min(post_date_gmt)) AS firstdate,
  9. YEAR(max(post_date_gmt)) AS lastdate
  10. FROM
  11. $wpdb->posts
  12. WHERE post_status = 'publish'
  13. ");
  14. if($copyright_dates) {
  15. $date = date('Y-m-d');
  16. $date = explode('-', $date);
  17. $copyright = "Copyright © " . $copyright_dates[0]->firstdate;
  18. if($copyright_dates[0]->firstdate != $date[0]) {
  19. $copyright .= '-' . $date[0];
  20. }
  21. echo $copyright;
  22. }
  23. }

那么我们在前端调用就可以填写:

  1. <?php copyrightDate();?>
© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
相关推荐
  • 暂无相关文章
评论 抢沙发