wordpress页脚修改
Often you will see a website that has an outdated copyright date which is pretty annoying. There are also sites that only show the current year for their copyright date which is even more annoying because you won’t know how old the site is. There is a simple PHP solution to this that most developers would know, but there is a more elegant way that we will show you. In this article, we will share a function that will automatically generate a copyright date based on the published date of your oldest and newest post.
通常,您会看到一个网站的版权日期过时,这很烦人。 也有一些网站仅显示其版权日期的当前年份,这更令人讨厌,因为您不知道该网站的年代。 大多数开发人员都会知道有一个简单PHP解决方案,但是有一种更优雅的方法可以向您展示。 在本文中,我们将共享一个功能,该功能将根据您最旧和最新帖子的发布日期自动生成版权日期。
简单的动态版权日期PHP解决方案 (Simple PHP Solution for Dynamic Copyright Date)
You would paste something like this in your theme’s functions.php file
您将在主题的functions.php文件中粘贴类似的内容
© 2009 – <?php echo date('Y'); ?> YourSite.com
The problem with this issue is that you would have to add this once your site is at least one year old.
这个问题的问题是,一旦您的网站至少成立了一年,您就必须添加它。
优雅的WordPress动态版权日期解决方案 (Elegant WordPress Solution for Dynamic Copyright Date)
While surfing the web, we saw a more elegant solution suggested by @frumph of CompicPress Theme. They are using this function on their excellent ComicPress theme. This function will generate a dynamic copyright date based on the published date of your oldest post and your newest post. If it is the first year of your site, then this function will only display the current year.
在网上冲浪时,我们看到了CompicPress Theme的@frumph建议的更优雅的解决方案。 他们在出色的ComicPress主题上使用了此功能。 此功能将根据您最旧文章和最新文章的发布日期生成动态版权日期。 如果这是您网站的第一年,则此功能将仅显示当前年份。
To implement this dynamic copyright date in your WordPress footer, open your theme’s functions.php file and add the following code:
要在WordPress页脚中实现此动态版权日期,请打开主题的functions.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 = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
Then open your theme’s footer.php file and add the following code where you want to display the date:
然后打开主题的footer.php文件,并在要显示日期的位置添加以下代码:
<?php echo comicpress_copyright(); ?>
This function will add the following text:
此函数将添加以下文本:
2009 – 2016
2009 – 2016
Don’t keep your copyright dates outdated. Take advantage of this technique in your current and future WordPress sites.
不要让您的版权日期过时。 在您当前和将来的WordPress网站中利用此技术。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-a-dynamic-copyright-date-in-wordpress-footer/
wordpress页脚修改