ワードプレスのis_new_day関数の使用方法・解説

概要

is_new_day 関数は、現在の投稿記事の投稿日が直前の投稿記事と同じかどうかを調べるための関数です。この関数は、投稿の日付によるコンテンツのグループ化やカスタムウィジェットにおける日付の表示など、以下のような用途でよく使用されます。

  1. 日付別の投稿リスト作成
  2. 日付ごとのアーカイブ表示
  3. ニュースフィードの整理
  4. カレンダーウィジェットや投稿の日付表示
  5. 新しい日付の投稿タグの追加
  6. グループ化された投稿の見出し表示
  7. 分類可能な投稿の表示管理
  8. デイリースタッツの追跡

構文

is_new_day( $previous, $current );

パラメータ

  • $previous: 比較する前の日付(DateTimestring形式で提供されます)
  • $current: 現在の日付(DateTimestring形式で提供されます)

戻り値

  • true: 現在の日付が新しい日である場合
  • false: 現在の日付が新しい日でない場合

関連する関数

使用可能なバージョン

is_new_day 関数は、WordPress 4.2.0 以降で使用できます。

コアファイルのパス

wp-includes/general-template.php

この関数のアクションでの使用可能性

アクション名 使用可能性
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプルコード1: 日付別の投稿リストを作成する

$previous_date = '';
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        $current_date = get_the_date('Y-m-d');
        if ( is_new_day( $previous_date, $current_date ) ) {
            echo '<h2>' . get_the_date() . '</h2>';
        }
        $previous_date = $current_date;
        the_title();
    }
}

このサンプルコードは、投稿日が異なる場合に日付の見出しを表示しながら投稿タイトルをリスト化します。

サンプルコード2: 最近の投稿を日付ごとに整理する

$previous_date = '';
$recent_posts = get_posts();
foreach ( $recent_posts as $post ) {
    setup_postdata($post);
    $current_date = get_the_date('Y-m-d');
    if ( is_new_day( $previous_date, $current_date ) ) {
        echo '<h2>' . get_the_date() . '</h2>';
    }
    echo '<li>' . get_the_title() . '</li>';
    $previous_date = $current_date;
}
wp_reset_postdata();

このサンプルでは、最近の投稿を取得し、日毎に整理してリスト化しています。

サンプルコード3: カスタムウィジェットで日付を表示する

class Custom_Date_Widget extends WP_Widget {
    public function widget( $args, $instance ) {
        $previous_date = '';
        $posts = get_posts(['numberposts' => 5]);
        foreach ( $posts as $post ) {
            setup_postdata( $post );
            $current_date = get_the_date('Y-m-d');
            if ( is_new_day( $previous_date, $current_date ) ) {
                echo '<h3>' . get_the_date() . '</h3>';
            }
            echo '<p>' . get_the_title() . '</p>';
            $previous_date = $current_date;
        }
        wp_reset_postdata();
    }
}

このコードはカスタムウィジェットを作成し、日付ごとに投稿のタイトルを表示します。

サンプルコード4: 投稿の日付で条件分岐する

$previous_date = '';
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        $current_date = get_the_date('Y-m-d');
        if ( is_new_day( $previous_date, $current_date ) ) {
            echo '<hr /><h2>' . get_the_date() . '</h2>';
        }
        echo '<div>' . get_the_content() . '</div>';
        $previous_date = $current_date;
    }
}

このサンプルコードでは、日付が変わるごとに見出しを区切りとして表示します。

サンプルコード5: コールバック関数内で使用する

function display_date_grouped_posts() {
    $previous_date = '';
    $args = ['posts_per_page' => 10];
    $posts = get_posts($args);
    foreach ( $posts as $post ) {
        setup_postdata($post);
        $current_date = get_the_date('Y-m-d');
        if ( is_new_day( $previous_date, $current_date ) ) {
            echo '<h2>' . get_the_date() . '</h2>';
        }
        echo '<p>' . get_the_title() . '</p>';
        $previous_date = $current_date;
    }
    wp_reset_postdata();
}

この関数は、投稿を取得し日付ごとに見出しを表示するカスタム関数として定義されています。

この関数について質問する


上の計算式の答えを入力してください