WordPress 小技巧: 给文章增加有效期功能
Friday October 24, 2008如果可以给文章设定一个有效期,在达到有效期后自动隐藏,这个功能可能有部分 WordPrses 爱好者需要吧。不过 WordPress 默认没有这个功能,今天在网上乱逛的时候偶然发现了一篇给文章增加有效期的文章,他是利用 WordPress 的custom field(自定义字段)的功能实现的。
实现方法需要修改你的主题文件,代码如下(注意:下面的代码为原文代码,只做参考用):
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> // 获取以 expiration 为 key 的 custom field 的值 $expirationtime = get_post_custom_values('expiration'); if (is_array($expirationtime)) { // 把数组元素组合为字符串 $expirestring = implode($expirationtime); } // 比较时间 $secondsbetween = strtotime($expirestring)-time(); if ( $secondsbetween > 0 ) { // For exemple... the_title(); the_excerpt(); } endwhile; endif; ?>
使用get_post_custom_values函数获取你在写日志时定义的以expiration为 key 的custom field的值,进行一定的格式化动作后和现在的时间进行比较,如果大于现在的时间,说明文章有效期还未到,则显示这篇日志。
需要注意的是,在写日志的时候,记得填写 custom field, key 为 expiration (和上面代码中的 expiration 相对应),value 为mm/dd/yyyy格式的时间,如10/28/2008 。
也许有人看了代码也不甚清楚,我就拿默认主题之一 default 作实例吧。修改index.php文件,下面是修改后的代码。
<?php /** * @package WordPress * @subpackage Default_Theme */ get_header(); ?> <div id="content" class="narrowcolumn"> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php $expirationtime = get_post_custom_values('expiration'); if(empty($expirationtime)){ ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> <div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div> <p class="postmetadata"> <?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p> </div> <?php }else{ if (is_array($expirationtime)) { $expirestring = implode($expirationtime); } $secondsbetween = strtotime($expirestring)-time(); if ( $secondsbetween > 0 ) { ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> <h2> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> <div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div> <p class="postmetadata"> <?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p> </div> <?php }} ?> <?php endwhile; ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div> </div> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php get_search_form(); ?> <?php endif; ?> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
文中肯定会有哪里有纰漏,请见谅,据说我不懂 php,
最后延伸一点,利用get_post_custom_values这个函数可以实现很多自定义显示方式,大家在制作主题时也许可以考虑一下
。

我也看不出纰漏,我也不懂PHP。
非常需要这一功能。过去,经常删除旧文,但很不方便,也有很多读者反对,所以在犹豫中。
试试此功能去。非常感谢分享。
删除旧文的理由呢?空间问题?
我也不懂php
自定义域的利用比较灵活的啊。觉得这个功能对需要定期置顶的文章有帮助。
我回来了哥们。
[...] 给文章增加有效期功能 [...]
@yvbo 你最近跑哪去了…
[...] 2. 给文章增加有效期功能 [...]
[...] 给文章增加有效期功能 [...]
[...] 给文章增加有效期功能 [...]
那么有没有文章定时发布功能呢.
如,我有10个文章,希望在10天内发布,每天自动发布一个.
就好象淘宝助理的定时发布商品功能一样.可以自已定义发布时间.
真诚请教.望指点. 祝上网快乐.谢谢.再次感谢.
@zjyhzj 我想 wp 有个预发布功能应该满足你的需求… 在你写好文章后, 你可以存为草稿,这是肯定知道的, 也可以直接发布,在 发布的按钮上面不是有个 edit 吗? 点那个 edit 可以修改发布时间,修改到你想发布的日期,然后点发布,这样就是预发布了,到了指定时间会自动发布的…
我回来了哥们。
我也不懂
不懂!!
[...] 给文章增加有效期功能 [...]
我也是win空间,但是首页老是会出现Sorry, but you are looking for something that isn’t here.就没有文章,但是刷新后又正常了,博主知道是怎么回事吗?