wordpress设定文章的显示时间期限或过期时间的标志
WordPress 时间 过期 显示 文章 设定 标志 期限
2023-06-13 09:15:38 时间
对于类似发布各种活动通知或到期时间内容的wordpress站点,也许会需要这样一个功能:发布活动内容的时候设定活动的到期日期,当活动还没有过期,网页显示“进行中”;当活动已过了设定的日期,网页则显示“已到期”或者不再显示该文章。有了这个功能,wordpress站长就不需要每次在活动过期后再编辑文章,实现的方法可以通过wordpress内置的自定义字段。
创建日期自定义字段:
字段名称使用:expiration
设定日期的格式必须是:mm/dd/yyyy 00:00:00 如:01/01/2015 00:00:00
修改主题模板:
编辑当前使用的主题模板,修改文章主循环代码:
<?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;
?>
上面代码的作用是如果当前时尚没有超过设定的时间内容中就显示“进行中”,否则就显示“已过期”。
相关文章
- wordpress模板代码详解
- wordpress完整搬家更换域名或者批量数据库修改字符的方法(wordpress如何更换域名)
- wordpress美化登录界面Wordpress博客 2 年前 桃李Taoli
- WordPress:Briefly unavailable for scheduled maintenance. Check back in a minute.
- WordPress头条搜索时间因子改造代码
- wordpress 定制仪表盘
- php更换wordpress用户头像,将用户本地上传头像功能集成到WordPress主题中
- WordPress 文章查询教程12:如何使用 Mime Type 和返回字段相关参数
- WordPress 允许用户编辑留言插件:Ajax Edit Comments
- 一文详解 robots meta 标签和 WordPress 中的应用
- WordPress 2.3 发布
- WordPress 插件推荐:Absolute Comments
- WordPress 主题教程 #14:底部和拆分 Index
- WordPress 技巧:屏蔽 REST API
- WordPress 技巧:使用 WP_Http 直接下载文件
- WordPress 技巧:设置回收站自动清理的时间间隔
- WordPress 技巧:通过 ID 快速获取日志内容
- WordPress 技巧:禁止插件更新提醒
- WordPress 内存缓存终极加速插件:Batcache
- WordPress判断用户是否登录的代码