方法一:
使用 WordPress 的 get_the_date() 函数来获取文章的发布日期
然后使用 PHP 的 strtotime() 函数来计算出文章发布的时间戳
最后使用 PHP 的 date() 函数来格式化日期
并使用 CSS 来设置文章发布日期的颜色
PHP代码如下:
<?php
$post_date = get_the_date();
$timestamp = strtotime($post_date);
$date = date('Y-m-d', $timestamp);
if (date('Y-m-d') == $date) {
echo ''.$date.'';
} else {
echo $date;
}
?>
方法二:
function highlight_today_published_date($the_date) {
$post_date = get_the_date('Y.m.d'); // 获取文章发布日期,使用"."作为分隔符
$current_date = date('Y.m.d'); // 获取当前日期,使用"."作为分隔符
// 检查文章发布日期是否为今日
if ($post_date === $current_date) {
// 将字体颜色设置为红色
$the_date = '' . $the_date . '';
} else {
$current_time = strtotime(current_time('Y-m-d')); // 获取当前日期的时间戳
$post_time = strtotime(str_replace(".", "-", $post_date)); // 替换分隔符并获取文章发布日期的时间戳
// 检查当前日期是否大于文章发布日期
if ($current_time > $post_time) {
// 将字体颜色设置为黑色
$the_date = '' . $the_date . '';
}
}
return $the_date;
}
add_filter('the_date', 'highlight_today_published_date');
2.前端调用日期代码如下:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_date('Y.m.d'); ?>