WordPress添加动态版权日期的方法

网站都会在页脚添加个类似Copyright ©2000-2024版权信息,如果每年都改这个日期,是比较麻烦的,而且很多时候,我们会忘记修改,别人提醒我们了,才知道没有修改,其实这是个细节方面的内容,可有些访客就很介意,如果不及时更新的话,可能会觉得该网站已经不维护了,那么我们该怎么去解决呢?可以通过下面的方法添加一个动态版权日期。

将下面代码添加到当前主题函数模板functions.php中:

function 鹿泽_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 鹿泽_copyright(); ?>

👋 感谢您的观看!

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享