WordPress
WordPressの新着記事に指定した日数NEWと表示する
March 9, 2018

記事一覧を表示する際、一目で新着記事だとわかるようにNewと表示する方法をご紹介。表示する日数も任意で変更可能、テキストだけではなく画像での表示も可能です。
以下をarchive.php
やcategory.php
に記述します。
<?php $days = 7; //表示させたい日数。ここでは7日 $post_entry = get_the_time('U'); $article = time() - ($days * 86400); if( $post_entry > $article ){ echo '<span class="new-tag">\ NEW! /</span>'; } ?>
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post();?> <a href="<?php the_permalink() ?>"> <figure> <?php the_post_thumbnail('full'); ?> <figcaption> <?php $days = 7; //表示させたい日数。ここでは7日 $post_entry = get_the_time('U'); $article = time() - ($days * 86400); if( $post_entry > $article ){ echo '<span class="new-tag">\ NEW! /</span>'; } ?> <h2><?php the_title(); ?></h2> </figcaption> </figure> </a> <?php endwhile; ?> <?php endif; ?>
<?php $days = 7; //表示させたい日数 $post_entry = get_the_time('U'); $article = time() - ($days * 86400); if( $post_entry > $article ){ echo '<img src="' .get_template_directory_uri('template_url'). '/img/new.png" width="100" height="100" alt="\ NEW! /" />'; } ?>