代码部署
文件路径:/wp-content/themes/zibll/func.php
添加如下代码。
( 后台外观 - 主题文件编辑器 - func.php
)
后台发布文章页面会多出个“文章有效期”栏目。填写好到期时间发布即可!
//文章有效期
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');
有问题及时联系站长,QQ:1240555208
更多优质资源在QQ群里,可以进群领取:467392290~
© 版权声明
THE END
暂无评论内容