WordPress: Agar post bugun chop etilgan bo'lsa, maxsus sinf qo'shing
Men doimiy ravishda mijozlarimizdan hech qachon ko'rib chiqmagan sozlashlar bo'yicha so'rovlar olaman. Yaqinda bizda o'z postlari uchun maxsus uslublar chop etilishini istagan mijozimiz bor edi bugun maxsus CSS klassi yordamida ularni ajratib ko'rsatish uchun. Ular sinfni arxiv sahifalarida, qidiruv natijalarida va bolalar mavzusining bitta post sahifasi shablonlarida qo'shishni xohlashdi.
Sozlash uchun <div>
Postning bugun yozilganligiga qarab WordPress shablonidagi sinfdan foydalanishingiz mumkin PHP va WordPress shablon faylingizdagi shablon teglari. Bunga qanday erishishingiz mumkinligiga misol keltiramiz:
<?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>
Ushbu kod parchasida biz postning sanasini solishtiramiz ($post_date
) joriy sana bilan ($current_date
). Agar ular mos kelsa, biz maxsus sinf tayinlaymiz (custom-today
) uchun $custom_class
o'zgaruvchan; aks holda, u bo'sh qoladi. O'zgartiring 'your-existing-classes'
Siz davom ettirmoqchi bo'lgan mavjud sinflar bilan <div>
. O'zingiz xohlagan qo'shimcha sinflarni qo'shing va shablon faylini saqlang.
Endi, bugun yozilgan postga tashrif buyurganingizda, <div>
element qo'shimcha sinfga ega bo'ladi custom-today
, CSS yordamida uni boshqacha uslublash imkonini beradi. Mana bir misol:
.custom-today {
background-color: yellow;
}
Mavzungiz davomida bir nechta misollar
Agar siz ushbu yondashuvni bir nechta mavzu fayllarida ishlatmoqchi bo'lsangiz, farzandingiz mavzusining functions.php fayliga maxsus funksiya qo'shishingiz mumkin:
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');
Keyin, har bir shablon ichida siz shunchaki qo'shishingiz mumkin post_class
:
<div <?php post_class(); ?>>
<!-- Your post content goes here -->
</div>
Vaqt zonalarini birlashtirish
Yuqoridagi misol tashrif buyuruvchining vaqti va mintaqasiga emas, balki WordPress serveringiz vaqti va mintaqasiga asoslangan sinfni qo'shadi. Agar siz foydalanuvchining vaqt mintaqasini kiritishni istasangiz… mana shu yerdasiz:
<?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>
Keshlash dinamik funksiyalarni amalga oshirishda kutilgan xatti-harakatlarga ta'sir qilishi mumkin, masalan, elementlarni joriy sana yoki tashrif buyuruvchi vaqt mintaqasi asosida sozlash. Keshlash veb-sahifalarning statik versiyalarini yoki ularga tezroq xizmat ko'rsatish uchun kontentni saqlash orqali veb-sayt ish faoliyatini yaxshilashga yordam beradi. Biroq, kontentni dinamik ravishda yangilash kerak bo'lganda, bu muammolarga olib kelishi mumkin.