很多WordPress部落格或網站,網頁上的版權時間都顯示的是建站時的年份,有些則是顯示當下的年份。事實上,這兩種方式都不是太好。

WordPress最好的顯示版權時間方式是顯示從網站建設之初的年份到目前的年份位置,類似© 2006 – 2010這種顯示方式。

這種效果通過以下的代碼可以實現。添加完下面的代碼後,系統會自動抓取發佈第一篇文章的年份以及最新一篇文章的年份,並把它顯示出來。

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 = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

把上面的代碼添加到了functions.php檔裡面後,還需要在 footer.php 任何你想顯示版權時間的地方加上如下代碼:

<?php echo comicpress_copyright(); ?>

相關文章