子比主题功能 – 为单个文章设置有效期,到期自动下线

admin
admin
管理员
532
文章
0
粉丝
综合评论111字数 347阅读1分9秒阅读模式

文件路径:/wp-content/themes/zibll/func.php  添加如下代码。

( 后台外观 - 主题文件编辑器 - func.php )文章源自小武站1z345.cn小武站-https://50-0.cn/846.html

后台发布文章页面会多出个“文章有效期”栏目。填写好到期时间发布即可!文章源自小武站1z345.cn小武站-https://50-0.cn/846.html

//文章有效期 by• www.foxccs.com
CSF::createMetabox('post_validity_options', array(
    'title'     => '文章有效期',
    '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'     => '设置内容的有效期,请确保格式正确,例如:<code>2020-10-10 23:59:59</code></br>到期后将会自动删除此此内容',
            '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>';
        $content  = '<div class="badg c-red block mb20"><i class="fa fa-clock-o mr10"></i>当前内容将于<span class="" data-over-text="' . esc_attr($over) . '"  data-countdown="' . $end_time . '"></span>后失效</div>' . $content;
    }

    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');

 文章源自小武站1z345.cn小武站-https://50-0.cn/846.html 文章源自小武站1z345.cn小武站-https://50-0.cn/846.html

继续阅读
  • 本文由 admin 发表于2024年8月17日 03:55:38
  • 转载请务必保留本文链接:https://50-0.cn/846.html
匿名

发表评论

匿名网友
确定