AI文摘 此内容由AI根据文章内容自动生成
AI Summary
为子比主题推添加文章有效期功能,允许您为每篇文章设定有效期。一旦到达设定时间,文章将自动下线,不再对外展示。这一功能不仅能帮助您有效管理内容时效性,还能提升用户体验,减少过期信息的干扰。轻松设置,高效管理,让您的网站内容始终保持新鲜与活力!
![图片[1]-子比主题美化 为文章设置有效期功能-到期自动下线-大海资源库](https://img.lzzcc.cn/i/2024/08/24/dvmkpn.webp)
代码部署
文件路径:/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');© 版权声明
NOTICE
1240555208@qq.com|Copyright © 2023~2026
01
DISCLAIMER
本站资源大多来自网络,如有侵犯权益请联系管理员,我们会第一时间审核删除。站内资源仅供学习测试,未经许可禁止商用,请在24小时内删除。
02
VIP PRIVILEGE
遇到付费内容?升级终身VIP即可全站免费畅享所有资源,可以联系我的微信进行开通。
大海聊天 QQ 3群:478065589
THE END








子比主题美化
精品源码资源 

暂无评论内容