內容營銷

WordPress:如果今天發布了帖子,請添加自定義類

我不斷收到客戶提出的定制請求,而我從未考慮過。 最近,我們有一位客戶想要為其發布的帖子添加一些自定義樣式 今日 以便可以使用自定義 CSS 類突出顯示它們。 他們希望將該類合併到其子主題的存檔頁面、搜索結果和單個帖子頁面模板中。

自定義 <div> 根據帖子是否是今天撰寫的 WordPress 模板中的類,您可以使用 PHPWordPress 模板文件中的模板標籤。 以下是如何實現此目標的示例:

<?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>

在此代碼片段中,我們比較帖子的日期($post_date) 與當前日期 ($current_date)。 如果它們匹配,我們分配一個自定義類(custom-today)到 $custom_class 多變的; 否則,它仍然是空的。 代替 'your-existing-classes' 與您想要保留的現有課程 <div>。 添加您想要的任何其他類並保存模板文件。

現在,當你訪問今天寫的一篇文章時, <div> 元素將具有附加類 custom-today,允許您使用 CSS 設置不同的樣式。 這是一個例子:

.custom-today {
background-color: yellow;
}

整個主題中的多個實例

如果您想在多個主題文件上使用此方法,您可以將自定義函數添加到子主題的functions.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');

然後,在每個模板中​​,您只需添加 post_class:

<div <?php post_class(); ?>>
    <!-- Your post content goes here -->
</div>

合併時區

上面的示例根據 WordPress 服務器的時間和時區(而不是訪問者的時間和時區)添加類。 如果您想要包含用戶的時區......就在這裡:

<?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>

在實現動態功能(例如根據當前日期或訪問者時區自定義元素)時,緩存可能會影響預期行為。 緩存通過存儲網頁或內容的靜態版本以更快地提供服務來幫助提高網站性能。 但是,當內容需要動態更新時,它可能會導致問題。

Douglas Karr

Douglas Karr 是 CMO 的 開放洞察 和創始人 Martech Zone。 道格拉斯幫助了數十家成功的 MarTech 新創公司,協助進行了超過 5 億美元的 MarTech 收購和投資盡職調查,並繼續協助公司實施和自動化其銷售和行銷策略。 道格拉斯是國際公認的數位轉型和 MarTech 專家和演講者。 道格拉斯也是一本傻瓜指南和一本商業領導書的出版作者。

相關文章

返回頂部按鈕
關閉

檢測到Adblock

Martech Zone 我們能夠免費為您提供這些內容,因為我們通過廣告收入、聯屬鏈接和讚助從我們的網站中獲利。 如果您在瀏覽我們的網站時刪除廣告攔截器,我們將不勝感激。