如我的博客,页脚版权时间是动态的,是根据文章发布时间区域来展现的,不是简简单单的在footer.php中加了“2011-2012”,是通过<?php echo comicpress_copyright(); ?>调用来实现的,不过也有些弊端,那就是假如你的博客好几年不更新的话,版权时间会停留在你发布的最后那篇文章那年,不过对于这么懒的博主来说,我没什么说的。

实现方法,添加以下代码到主题文件function.php中

//显示网站版权时间
function comicpress_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 = "&copy; " . $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 comicpress_copyright(); ?>

最后修改:2019 年 06 月 06 日
如果觉得我的文章对你有用,请随意赞赏