WordPressで表示する記事が新着かどうかを判別するコード

新着情報などにNewマークを表示させたいなどの要望はよくあることですね。
今回は、表示する記事が新着や更新かどうかの判別用コードを紹介します。

なるべく汎用的となるように書いたので、ちょっとコードが長いですが、ご容赦ください。
いつものように CODE 1 を利用するテーマの functions.php にぺたりと追加してもらえれば、新着や更新の条件判別ができるようになります。利用方法は、コードの後で。

CODE 1

/**
 * post_classの出力するclassに、新着であれば、new-post、更新であれば、modified-postを追加する。
 *
 * @param array $classes Array of default classes.
 * @param array|string $class array or string space separated class names. 
 * @param int $post_id post ID.
 * @return array Array of marged classes.
 */
function new_and_modefied_post_class( $classes, $class, $post_id ) {
	$post_id = (int)$post_id;
	$post = get_post( $post_id );

	if ( is_new_post() ) {
		$classes[] = 'new-post';
	}
	if ( is_modified_post() && $post->post_modified > $post->post_date ) {
		$classes[] = 'modified-post';
	}
	return $classes;
}
add_filter( 'post_class', 'new_and_modefied_post_class', 10, 3 );

/**
 * 新着かどうかを判別する。
 *
 * @param string $post_date PHP date format.
 * @param int $days days of new period.
 * @return bool within or not
 */
function is_new_post( $post_date = '', $days = 0 ) {
	global $post;
	if ( ! $post_date ) {
		$post_date = $post->post_date;
	}
	if ( ! $days ) {
		$days = absint( get_option( 'new_days', 7 ) );
	}
	return is_widthin_days( $post_date, $days );
}

/**
 * 更新かどうかを判別する。
 *
 * @param string $post_date PHP date format.
 * @param int $days days of modified period.
 * @return bool within or not
 */
function is_modified_post( $post_date = '', $days = 0 ) {
	global $post;
	if ( ! $post_date ) {
		$post_date = $post->post_modified;
	}
	if ( ! $days ) {
		$days = absint( get_option( 'modified_days', 7 ) );
	}
	return is_widthin_days( $post_date, $days );
}

/**
 * 期間内かどうかを判別する。
 *
 * @param string $post_date PHP date format.
 * @param int $days days of period.
 * @return bool within or not
 */
function is_widthin_days( $post_date, $days = 7 ) {
	if ( in_array( strtotime( $post_date ), array( false, -1 ) ) ) {
		return false;
	}

	$limit = current_time( 'timestamp' ) - ( $days - 1 ) * 24 * 3600;
	if ( mysql2date( 'Y-m-d', $post_date ) >= date( 'Y-m-d', $limit ) ) {
		return true;
	}
	return false;
}

/**
 * 表示設定ページに新着と更新の表示期間(日数)の設定項目を追加する
 */
function add_days_items() {
	add_settings_field( 'new_days', '新着期間設定', 'display_new_days_field', 'reading' );
	add_settings_field( 'modified_days', '更新期間設定', 'display_modified_days_field', 'reading' );
}
add_action( 'admin_init', 'add_days_items' );

/**
 * 設定で保存できる項目に、新着表示日数と更新表示日数を追加する
 *
 * @param array $whitelist_options.
 * @return array filtered whitelist options
 **/
function allow_new_and_modified_post_data( $whitelist_options ) {
	$whitelist_options['reading'][] = 'new_days';
	$whitelist_options['reading'][] = 'modified_days';
	return $whitelist_options;
}
add_filter( 'whitelist_options', 'allow_new_and_modified_post_data' );

/**
 * 表示設定画面に新着表示日数の設定項目を表示する
 */
function display_new_days_field() {
	$new_days = absint( get_option( 'new_days', 7 ) );
?>
	<input type="text" name="new_days" id="new_days" size="1" value="<?php echo esc_attr( $new_days ); ?>" />
	日間<span class="description">(1日間だと本日のみ、1週間にするには7日間としてください。)</span>
<?php
}

/**
 * 表示設定画面に更新表示日数の設定項目を表示する
 */
function display_modified_days_field() {
	$modified_days = absint( get_option( 'modified_days', 7 ) );
?>
	<input type="text" name="modified_days" id="modified_days" size="1" value="<?php echo esc_attr( $modified_days ); ?>" />
	日間<span class="description">(1日間だと本日のみ、1週間にするには7日間としてください。)</span>
<?php
}

使い方

新着および更新の期間を設定する

管理画面の表示設定に、新着期間設定および更新期間設定の2項目が追加されるので、それぞれ有効な日数を設定してください。1日だと表示している日のみ有効となります。無効にしたい場合は 0日間、1週間とする場合は7日になります。

post_classに追加されるclassを利用して表示する

CODE 1を追加して期間の設定を行うと、テンプレートタグの post_class で出力される classに、新着であれば new-post、更新であれば、modified-post が出力されるようになります。CSSを用いて、new-post や modified-post で適切なアイコンなどが表示されるようにしてみてください。

post_classの出力例

<article id="post-1804" class="post-1804 post type-post status-publish format-standard hentry category-1 new-post modified-post">

判別用の条件分岐タグを使う

新着かどうか判別するを is_new_post、更新かどうかを判別する is_modified_post を使うと、Newアイコンを画像で表示することが可能となります。

新着かどうかを判別して、Newアイコンを表示する

<?php if ( is_new_post() ) : ?>
	<img src="new-icon.png" width="50" height="19" alt="New" />
<?php endif; ?>

更新かどうかを判別して、更新アイコンを表示する

<?php if ( is_modified_post() ) : ?>
	<img src="update-icon.png" width="50" height="19" alt="Update" />
<?php endif; ?>

応用編:ループ外で利用する方法

is_new_post と is_modified_post にパラメーターを指定すれば、WordPress ループの外(たとえばサイドバーの新着リストなど)でも利用できるようになります。ここで指定するパラメーターは、判定する日付となります。

ループ外で新着かどうかを判定

<?php if ( is_new_post( $new_post->post_date ) ) : ?>

応用編:設定した期間を異なる期間で判定

is_new_post と is_modified_post に2つ目のパラメーターを指定すれば、管理画面で設定した期間とは異なる期間での判定を行うことができます。

3日以内の更新かどうかを判定

<?php if ( is_modified_post( $new_post->post_modified, 3 ) ) : ?>

「WordPressで表示する記事が新着かどうかを判別するコード」への1件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です