分类分类
更新时间:2026-09-29 17:59:02作者:zhao
对于类似发布各种活动通知或到期时间内容的wordpress站点,也许会需要这样一个功能:发布活动内容的时候设定活动的到期日期,当活动还没有 过期,网页显示“进行中”;当活动已过了设定的日期,网页则显示“已到期”或者不再显示该文章。有了这个功能,wordpress站长就不需要每次在活动 过期后再编辑文章,实现的方法可以通过wordpress内置的自定义字段。
创建日期自定义字段:
字段名称使用:expiration
设定日期的格式必须是:mm/dd/yyyy 00:00:00 如:01/01/2015 00:00:00
修改主题模板:
编辑当前使用的wordpress模板,修改文章主循环代码:
<?php
if (have_posts()) :
while (have_posts()) : the_post();
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
?>
<div class="post" id="post-<?php the_ID();?>">
<h2><?php the_title();?></h2>
<div class="entry">
<?php the_excerpt();?>
</div>
</div>
<?php
}
endwhile;
endif;
?>
上面代码的作用是如果当前时间超过设定的时间,文章则不显示。
编辑当前使用的主题模板,修改文章主循环代码:
<?php
if (have_posts()) :
while (have_posts()) : the_post();
?>
<div class="post" id="post-<?php the_ID();?>">
<h2><?php the_title();?></h2>
<div class="entry">
<?php the_excerpt();?>
<?php
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
echo '进行中';
}else {
echo '已过期';
}
?>
</div>
</div>
<?php
endwhile;
endif;
?>
上面代码的作用是如果当前时尚没有超过设定的时间内容中就显示“进行中”,否则就显示“已过期”。
相关
可露优世界休闲益智790.16 Mv9.92.31.102026-09-29
下载OverPeak超人游戏角色扮演304.38 Mv0.62026-09-29
下载宇宙回形针休闲益智24.07 Mv2.1.282026-09-29
下载pvzrhlauncher启动器手游辅助91.98 Mv1.1.1-ci.5492026-09-29
下载玛雅纪元角色扮演884.12 Mv2.1.122026-09-29
下载倒水倒不停红包版休闲益智68.28 Mv1.0.42026-09-29
下载观心心理(心理成长服务)生活服务208.9 MBv2.0.42026-09-29
下载速查驾规(交通法规速查)生活服务91.6 MBv1.0.02026-09-29
下载高能及时导航2026官方最新版本生活服务68.8 MBv1.0.02026-09-29
下载AI语音智能帮手2026最新版本社交通讯83.0 MBv1.0.12026-09-29
下载Nuna智能吊坠APP(健康情绪助手)生活服务190.3 MBv3.8.02026-09-29
下载开锁豹(锁匠接单平台)生活服务42.2 MBv1.0.52026-09-29
下载










