WordPress: Onjezani Kalasi Yachizolowezi Ngati Post Idasindikizidwa Lero
Nthawi zonse ndimalandira zopempha kuchokera kwamakasitomala athu pazosintha zomwe sindinaziganizirepo. Posachedwapa, tinali ndi kasitomala yemwe amafuna masitayelo amtundu wa zomwe amalemba lero kuti athe kuwunikira pogwiritsa ntchito kalasi ya CSS. Ankafuna kuphatikizira kalasiyo pamasamba osungira zakale, zotsatira zosaka, ndi ma tempulo atsamba limodzi lamutu wamwana wawo.
Kuti makonda a <div>
kalasi mu WordPress template kutengera ngati positi idalembedwa lero, mutha kugwiritsa ntchito Php ndi WordPress ma tag a template mkati mwa fayilo yanu ya template. Nachi chitsanzo cha momwe mungakwaniritsire izi:
<?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>
Pachidule ichi, tikufanizira tsiku la positi ($post_date
) ndi tsiku lapano ($current_date
). Ngati zikugwirizana, timagawira gulu lachizolowezi (custom-today
) kwa $custom_class
kusintha; apo ayi, imakhalabe yopanda kanthu. M'malo 'your-existing-classes'
ndi makalasi omwe alipo omwe mukufuna kupitiliza <div>
. Onjezani makalasi ena owonjezera omwe mukufuna ndikusunga fayilo ya template.
Tsopano, mukamayendera positi yomwe idalembedwa lero, ndi <div>
element idzakhala ndi kalasi yowonjezera custom-today
, kukulolani kuti muyipange mosiyana pogwiritsa ntchito CSS. Nachi chitsanzo:
.custom-today {
background-color: yellow;
}
Zochitika Zambiri Pamutu Wanu Wonse
Ngati inu ankafuna kugwiritsa ntchito njira imeneyi pa angapo owona owona, inu mukhoza kuwonjezera mwambo ntchito kwa mwana wanu mutu file function.php:
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');
Kenako, mkati mwa template iliyonse, mutha kungowonjezera post_class
:
<div <?php post_class(); ?>>
<!-- Your post content goes here -->
</div>
Kuphatikiza Magawo a Nthawi
Chitsanzo pamwambapa chikuwonjezera kalasi kutengera nthawi ya seva yanu ya WordPress ndi nthawi yake, osati nthawi ndi nthawi ya mlendo. Ngati mumafuna kuti nthawi ya wosuta ikhalepo… nayi:
<?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>
Kusungitsa nkhokwe kumatha kukhudza zomwe zikuyembekezeka mukakhazikitsa magwiridwe antchito monga kusintha zinthu malinga ndi tsiku lomwe lilipo kapena nthawi ya alendo. Caching imathandizira kukonza magwiridwe antchito awebusayiti posunga masamba osasintha kapena zomwe zili patsamba kuti ziwathandize mwachangu. Komabe, zimatha kuyambitsa zovuta pamene zomwe zili zofunika kusinthidwa mwamphamvu.