對(duì)于類似發(fā)布各種活動(dòng)通知或到期時(shí)間內(nèi)容的wordpress站點(diǎn),也許會(huì)需要這樣一個(gè)功能:發(fā)布活動(dòng)內(nèi)容的時(shí)候設(shè)定活動(dòng)的到期日期,當(dāng)活動(dòng)還沒有 過期,網(wǎng)頁顯示“進(jìn)行中”;當(dāng)活動(dòng)已過了設(shè)定的日期,網(wǎng)頁則顯示“已到期”或者不再顯示該文章。有了這個(gè)功能,wordpress站長(zhǎng)就不需要每次在活動(dòng) 過期后再編輯文章,實(shí)現(xiàn)的方法可以通過wordpress內(nèi)置的自定義字段。
創(chuàng)建日期自定義字段:
字段名稱使用:expiration
設(shè)定日期的格式必須是:mm/dd/yyyy 00:00:00 如:01/01/2015 00:00:00
修改主題模板:
編輯當(dāng)前使用的wordpress模板,修改文章主循環(huán)代碼:
<?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;
?>
上面代碼的作用是如果當(dāng)前時(shí)間超過設(shè)定的時(shí)間,文章則不顯示。
編輯當(dāng)前使用的主題模板,修改文章主循環(huán)代碼:
<?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 '進(jìn)行中';
}else {
echo '已過期';
}
?>
</div>
</div>
<?php
endwhile;
endif;
?>
上面代碼的作用是如果當(dāng)前時(shí)尚沒有超過設(shè)定的時(shí)間內(nèi)容中就顯示“進(jìn)行中”,否則就顯示“已過期”。
新聞熱點(diǎn)
疑難解答
圖片精選