WordPress: Ychwanegu Dosbarth Personol Os Cyhoeddwyd Y Post Heddiw
Rwy'n cael ceisiadau gan ein cleientiaid yn gyson am addasiadau nad wyf erioed wedi eu hystyried. Yn ddiweddar, roedd gennym gleient a oedd eisiau rhywfaint o steilio personol ar gyfer eu postiadau wedi'u cyhoeddi heddiw fel y gellir eu hamlygu gan ddefnyddio dosbarth CSS wedi'i deilwra. Roeddent am ymgorffori'r dosbarth ar dudalennau archif, canlyniadau chwilio, a thempledi tudalennau post sengl o'u thema plentyn.
I addasu'r <div>
dosbarth mewn templed WordPress yn seiliedig ar a ysgrifennwyd y post heddiw, gallwch ei ddefnyddio PHP ac WordPress tagiau templed yn eich ffeil templed. Dyma enghraifft o sut y gallwch chi gyflawni hyn:
<?php
// Get the current post's date
$post_date = get_the_date('Y-m-d');
// Get today's date
$current_date = date('Y-m-d');
// Check if the post was written today
if ($post_date === $current_date) {
$today_class = 'custom-today';
} else {
$today_class = '';
}
?>
<div class="your-existing-classes <?php echo $today_class; ?>">
<!-- Your post content goes here -->
</div>
Yn y pyt cod hwn, rydym yn cymharu dyddiad y postiad ($post_date
) gyda'r dyddiad cyfredol ($current_date
). Os ydynt yn cyfateb, rydym yn aseinio dosbarth wedi'i deilwra (custom-today
) i'r $custom_class
amrywiol; fel arall, mae'n parhau i fod yn wag. Amnewid 'your-existing-classes'
gyda'r dosbarthiadau presennol yr ydych am eu cadw ar y <div>
. Ychwanegwch unrhyw ddosbarthiadau ychwanegol y dymunwch ac arbedwch y ffeil templed.
Nawr, pan fyddwch yn ymweld â phost a ysgrifennwyd heddiw, mae'r <div>
Bydd yr elfen yn cael y dosbarth ychwanegol custom-today
, sy'n eich galluogi i'w steilio'n wahanol gan ddefnyddio CSS. Dyma enghraifft:
.custom-today {
background-color: yellow;
}
Achosion Lluosog Trwy gydol Eich Thema
Os oeddech chi eisiau defnyddio'r dull hwn ar sawl ffeil thema, gallwch ychwanegu swyddogaeth wedi'i haddasu i ffeil functions.php thema eich plentyn:
function add_custom_class_based_on_date($classes) {
// Get the current post's date
$post_date = get_the_date('Y-m-d');
// Get today's date
$current_date = date('Y-m-d');
// Check if the post was written today
if ($post_date === $current_date) {
$classes[] = 'custom-today';
}
return $classes;
}
add_filter('post_class', 'add_custom_class_based_on_date');
Yna, o fewn pob templed, gallwch chi ychwanegu post_class
:
<div <?php post_class(); ?>>
<!-- Your post content goes here -->
</div>
Ymgorffori Parthau Amser
Mae'r enghraifft uchod yn ychwanegu'r dosbarth yn seiliedig ar amser a chylchfa amser eich gweinydd WordPress, nid amser ac ardal amser yr ymwelydd. Os oeddech am i gylchfa amser y defnyddiwr gael ei chynnwys… dyma chi:
<?php
// Get the current post's date and convert it to the visitor's timezone
$post_date = get_the_date('Y-m-d');
$post_date_timezone = get_post_time('O');
$post_date_timezone_offset = substr($post_date_timezone, 0, 3) * 3600 + substr($post_date_timezone, 3, 2) * 60;
$current_date = date('Y-m-d', current_time('timestamp', false));
$current_date_timezone = get_option('timezone_string');
$current_date_timezone_offset = get_option('gmt_offset') * 3600;
// Calculate the offset between the post date and the current date based on timezones
$timezone_offset = $current_date_timezone_offset - $post_date_timezone_offset;
// Adjust the post date by the timezone offset
$post_date_adjusted = date('Y-m-d', strtotime($post_date) + $timezone_offset);
// Check if the post was written today
if ($post_date_adjusted === $current_date) {
$today_class = 'custom-today';
} else {
$today_class = '';
}
?>
<div class="your-existing-classes <?php echo $today_class; ?>">
<!-- Your post content goes here -->
</div>
Gall caching effeithio ar yr ymddygiad disgwyliedig wrth weithredu ymarferoldeb deinamig fel addasu elfennau yn seiliedig ar y dyddiad cyfredol neu gylchfa amser yr ymwelydd. Mae caching yn helpu i wella perfformiad gwefan trwy storio fersiynau statig o dudalennau gwe neu gynnwys i'w gwasanaethu'n gyflymach. Fodd bynnag, gall achosi problemau pan fydd angen diweddaru'r cynnwys yn ddeinamig.