我知道你很可能不會找一個PHP函數,但這裏是我做了什麼,這可以被添加到的functions.php https://gist.github.com/Garconis/11e7e4c316b4f5c4e5dfcbe7d916f5c3
<?php
// Add the times and days to the body classes
// e.g., date-day-wednesday date-ymd-20170614 date-month-june time-military-1317 time-pm time-early-afternoon
add_filter('body_class', 'add_date_info_to_class_names');
function add_date_info_to_class_names($classes) {
// current day of week name
$classes[] = 'date-day-' . strtolower(date_i18n('l'));
// current full date YYYYMMDD
$classes[] = 'date-ymd-' . strtolower(date_i18n('Ymd'));
// current month name
$classes[] = 'date-month-' . strtolower(date_i18n('F'));
// current military time HHMM
$classes[] = 'time-military-' . strtolower(date_i18n('Hi'));
// check hour and add am or pm class
if (date_i18n('H') >= 12){
$classes[] = 'time-pm';
} else {
$classes[] = 'time-am';
}
// check hour and add time of day descriptor
if (preg_match("/(00|01|02)/i", date_i18n('H'))){
$classes[] = 'time-early-night';
}
elseif (preg_match("/(03|04|05)/i", date_i18n('H'))){
$classes[] = 'time-late-night';
}
elseif (preg_match("/(06|07|08)/i", date_i18n('H'))){
$classes[] = 'time-early-morning';
}
elseif (preg_match("/(09|10|11)/i", date_i18n('H'))){
$classes[] = 'time-late-morning';
}
elseif (preg_match("/(12|13|14)/i", date_i18n('H'))){
$classes[] = 'time-early-afternoon';
}
elseif (preg_match("/(15|16|17)/i", date_i18n('H'))){
$classes[] = 'time-late-afternoon';
}
elseif (preg_match("/(18|19|20)/i", date_i18n('H'))){
$classes[] = 'time-early-evening';
}
elseif (preg_match("/(21|22|23)/i", date_i18n('H'))){
$classes[] = 'time-late-evening';
}
return $classes;
}
我編輯如果(n = 21) jQuery('body') .addClass( '夜間');以及它不起作用 – Ivan
http://jsbin.com/econuh/1 - 你的代碼,因爲它在原來的問題工作得很好。如果它在你的實現中不起作用,那麼其他的東西就會導致這個問題,我們需要更多的信息。 – sachleen
我認爲Wordpress正在拖動我...我不確定它是如何工作的,你可以用檢查www.powerupware.com時間來試試它。js和style.css的第一行 – Ivan