WordPressの最新記事に一定期間「NEW」アイコンを表示する方法

最新記事に一定期間「NEW」アイコンを表示する方法をご紹介します。

サンプルコード
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <dl> <dt> <a href="<?php the_permalink() ?>"><img src="<?php echo catch_that_image_thumb(); ?>" alt="<?php the_title(); ?>" /></a> <?php $days = 5; $today = date_i18n('U'); $entry_day = get_the_time('U'); $keika = date('U',($today - $entry_day)) / 86400; if ( $days > $keika ): echo '<span class="bs_new">NEW</span>'; endif; ?> </dt> <dd> </dd> <dl> <?php endwhile; else : ?> <?php endif; ?> |
NEWアイコンを表示させる期間は、6行目の
$days = 5;
の箇所で調整出来ます。
$days = 5; とした場合、記事の公開日から5日間、NEWアイコンが表示されます。
上記のサンプルコードでは、dtの画像部分にspanで囲んだNEWアイコンが重なるよう、
cssのpositionで配置しています。
![MARKLEAPS[マークリープス]](https://markleaps.com/blog/wp-content/themes/mkl/images/00_logo.png)

![[WordPress] プラグイン「WP ALL Import」で既存記事を一括更新(CSV上書き)する方法](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-all-import_update-500x254.jpg)
![[WordPress] CSVで記事を一括インポート-プラグイン「WP ALL Import」の使い方](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-all-import-500x254.jpg)
![[WordPress] 記事への画像挿入方法](https://markleaps.com/blog/wp-content/uploads/2025/10/pic_manual_mainimg-500x254.png)
![[WordPress] カテゴリーとタグの違いと設定方法](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-category-tag-500x254.jpg)