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

综合评论4,505,998字数 347阅读1分9秒

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

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

后台发布文章页面会多出个“文章有效期”栏目。填写好到期时间发布即可!文章源自小武站https://1z345.cn/小武站-https://bbs.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');

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

继续阅读
<玫瑰的故事>电视剧:多面解读与深度剖析
<玫瑰的故事>电视剧:多面解读与深度剖析 综合

<玫瑰的故事>电视剧:多面解读与深度剖析

玫瑰的故事 电视剧《玫瑰的故事》是一部备受关注的电视剧。该剧由刘亦菲、佟大为、林更新、万茜、林一、彭冠英、霍建华、朱珠等众多知名演员主演。由汪俊执导,李潇、王思编剧,陈金飞监制,于 2024 年 6 ...
  • 本文由 admin 发表于 2024年8月17日 03:55:38
  • 转载请务必保留本文链接:https://bbs.50-0.cn/846.html
  • 有效期
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定