Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

WordPress设定文章的显示时间期限或过期时间的标志

wordpress教程:设定文章的显示时间期限或过期时间的标志!对于类似发布各种活动通知或到期时间内容的wordpress站点,也许会需要这样一个功能:发布活动内容的时候设定活动的到期日期,当活动还没有 过期,网页显示“进行中”;当活动已过了设定的日期,网页则显示“已到期”或者不再显示该文章。

wordpress教程:设定文章的显示时间期限或过期时间的标志!对于类似发布各种活动通知或到期时间内容的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;
?>

上面代码的作用是如果当前时尚没有超过设定的时间内容中就显示“进行中”,否则就显示“已过期”。

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
编程技巧

织梦教程如何将上一篇下一篇的文字改为英文

2023-7-29 0:00:00

编程技巧

织梦教程如何在channel标签中调用栏目描述[field:description/]

2023-8-7 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索