熱評
加載中...
當前位置:首頁技術館建站教學二開美化【Wordpress】單個文章設置有效期,到期自動下線美化版

【Wordpress】單個文章設置有效期,到期自動下線美化版

使用教學

將代碼放在func.php,然後在後台編輯或新建文章,有一個文章有效期模組

【Wordpress】單個文章設置有效期,到期自動下線美化版

保存或更新後,前台對應文章就會有一個倒計時。 時間一到,自動進入回收站。

原文的倒計時我覺得有一點簡單,所以我使用AI修改了一下,美化圖如下:

【Wordpress】單個文章設置有效期,到期自動下線美化版

另外,原文模組是在文章上面,我給修改到下面了。

代碼

PHP
<?php 
//文章有效期
CSF::createMetabox('post_validity_options', array(
    'title'     => '<strong style="text-align:center;">文章有效期</strong>',
    'post_type' => ['post', 'page', 'forum_post'],
    'context'   => 'side',
    'data_type' => 'unserialize',
));
CSF::createSection('post_validity_options', array(
    'fields' => array(
        array(
            'id'       => 'post_validity_time',
            'type'     => 'date',
            'desc'     => '<strong style="text-align:center;">設置內容的有效期,請確保格式正確,例如:<code>2020-10-10 23:59:59</code><br>到期後將會自動刪除此此內容</strong>',
            'settings' => array(
                'dateFormat'  => 'yy-mm-dd 23:59:59',
                'changeMonth' => true,
                'changeYear'  => true,
            ),
        ),
    ),
));

function zib_the_content_fore($content)
{
    $post_id = get_the_ID();
    $time    = get_post_meta($post_id, 'post_validity_time', true);

    if ($time) {
        $end_time = date("m/d/Y H:i:s", strtotime($time));
        $over     = '<script type="text/javascript">window.location.reload()</script>';
$warning_div = '<div class="warning-box" style="text-align: center; border: 2px dashed lightgray; padding: 10px; border-radius: 18px; margin-bottom: 15px;"><i class="fa fa-exclamation-triangle" style="font-size: 23px;"></i>   <strong style="text-align:center;font-size: 23px;">AngelaL博客提醒您:因時效性問題,當前項目將於  <span class="countdown-style" style="color: red;background-color: black;border-radius: 5px;font-size: 23px;" data-over-text="'. esc_attr($over). '" data-countdown="'. $end_time. '"></span>  後自動刪除</strong></div>';
        if (!wp_is_mobile()) {
            $content.= $warning_div;
        }
    }
    return $content;
}
add_filter('the_content', 'zib_the_content_fore');

function zib_template_redirect_execute()
{
    if (is_single()) {
        $post_id = get_the_ID();
        $time    = get_post_meta($post_id, 'post_validity_time', true);

        if ($time && strtotime($time) < strtotime(current_time('Y-m-d H:i:s'))) {

            //執行刪除文章
            wp_trash_post($post_id);

            wp_safe_redirect(home_url());
            exit;
        }

    }
}
add_action('template_redirect', 'zib_template_redirect_execute');

您已閱讀完《WordPress(共53篇)》專題的第 22 篇。請繼續閱讀該專題下面的文章:

AngelaL博客

       本文是原創文章,採用CC BY-NC-SA 4.0協議,完整轉載請註明來自AngelaL博客

給TA打賞
共{{data.count}}人
人已打賞
二開美化

【Wordpress】功能表緩存外掛程式

2025-2-27 10:25:55

二開美化

解決Alist打開網站頁面載入緩慢問題

2025-2-27 13:25:19

0 條回复 A文章作者 M管理员
    
    暫無討論,說說你的看法吧
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索