Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressで今日の日付を表示する方法(2つの簡単な方法)

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressで今日の日付を表示したいですか?

多くのニュースサイト、オンラインジャーナル、頻繁に更新されるブログでは、現在の日付と時刻を表示したい場合があります。これによってユーザーは、現在の日付と、コンテンツが公開されてからどれくらい経ったかを知ることができる。

この投稿では、WordPressサイトに今日の日付や現在時刻を簡単に表示する方法を紹介します。

Displaying current date and time in WordPress

WordPressで今日の日付を表示する理由

多くのニュースサイトでは、サイトのヘッダー部分に現在の日付が表示される。特に、主要な記事を毎日公開している小規模なニュースサイトではそうだ。

A news website showing current date in their website header

これにより、ユーザーはその出版物のオンライン版の最新版を表示していることを確認できる。これは、1日に何度もトップページを更新しているにもかかわらず、多くのニュースサイトがいまだに使用している便利なスタイル決定である。

同様に、オンライン・ジャーナルやブロガーもこのスタイルを採用している。これにより、ユーザーはブログの最新エントリーを表示していることを保証される。

他にも、現在の日付と時刻を表示したいだけの使用シーンもあるだろう。

例えば、あなたのライブチャットが特定の時間に動作し、あなたの地理的な場所で何時であるかをカスタマイザーに表示したい場合。または、カウントダウン タイマー キャンペーンを実行しており、より強い FOMO 効果を作成するために現在の日付を表示したい場合。

ということで、WordPressサイトに今日の日付、曜日、または現在時刻を簡単に表示する方法を見てみましょう。ここでは2つの方法を取り上げますので、以下のクイックリンクから使いたい方にジャンプしてください:

方法1:テンプレートファイルにコードを追加して今日の日付を表示する

WordPressには、現在の日付や時刻を表示するウィジェットやブロックが初期設定されていません。

しかし、非常に簡単なコードを使えば、現在の日付や時刻を表示することはできる。

WordPressテーマのテンプレートファイルの時刻を表示したい場所に、この簡単なコードを追加することができます:

<?php echo date(get_option('date_format')); ?>

このコードは、WordPressの設定で設定されている日付フォーマットを使って現在の日付を表示します。日付の書式は設定 ” 一般ページで変更できます。

Time format settings in WordPress

また、独自の書式タグを使って、他の書式で日付を出力することもできます。例えば、以下のコードを使えば、月、日、年のフォーマットで日付を表示することができます。

<?php echo date('F j, Y'); ?>

デモサイトではこんな感じ。

Date and time

この方法では、WordPressテーマファイルに直接コードを追加する権限がありませんが、柔軟性に欠けます。WordPressの投稿、ページ、サイドバーウィジェットの中に現在の日付と時刻を表示したい場合はどうすればいいでしょうか?

次の方法では、サイトのどこにでも日付と時刻を追加することができます。

方法2:ショートコードを使ってどこでも今日の日付を表示する(推奨)

この方法では、ショートコードを作成し、WordPressサイト上の任意の場所に日付と時刻を表示するために使用します。

上級ユーザーは、テーマのfunctions.phpファイルに直接コードを追加することができます。しかし、WPCodeのようなカスタムコードスニペットプラグインを使用することをお勧めします。

最初に行う必要があるのは、無料のWPCodeプラグインをインストールすることです。詳しい手順については、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

有効化したら、WordPressダッシュボードのCode Snippets ” + Add Snippetに移動します。これでサイトに新しいコードスニペットを追加できます。

Adding Custom Code in WPCode

カスタムコードを追加(新規スニペット)」オプションにマウスオーバーし、表示される「スニペットを使用」ボタンをクリックします。新しいウィンドウが開き、そこにコード・スニペットを追加することができます。

まず、スニペットに「今日の日付を追加」のようなタイトルを追加する。

Add a Title and the Code Snippet to WPCode

次に、上のスクリーンショットにあるように、以下のコード・スニペットをコピー&ペーストして、コード・プレビュー・フィールドに入力します。

function wpb_date_today( $atts, $content = null ) {
	$atts = shortcode_atts( array(
		'format' => '',
	), $atts );

	$date_time = '';

	if ( $atts['format'] == '' ) {
		$date_time .= date( get_option( 'date_format' ) );
	} else {
		$date_time .= date( $atts['format'] );
	}

	return $date_time;
}

add_shortcode( 'date-today', 'wpb_date_today' );

最後に、コードタイプドロップダウンメニューから「PHP Snippet」を選択し、有効化トグルを「On」の位置に切り替えます。

これで「スニペットを保存」ボタンをクリックして、サイトにコード・スニペットを保存することができます。

Choose the PHP Code Type and Toggle the Snippet On in WPCode

このコードは、現在の日付を表示するショートコードを作成するだけです。このショートコードをサイトの任意の場所に追加することで使用できます:

[日付-今日]

初期設定では、ショートコードはWordPressの設定にあるデフォルトの日付形式で日付を表示します。

このようにショートコードを修正することで、独自の日付フォーマットを使用することもできます:

[date-today format='F j, Y']。

そして、あなたのサイトではこのように表示されるはずです。

Date preview

WordPressで日付を表示するエキスパートガイド

WordPressで今日の日付を表示する方法はおわかりいただけたと思いますが、WordPressの日付表示に関連する他のガイドもご覧ください:

WordPressで今日の日付を簡単に表示する方法について、この投稿がお役に立てば幸いです。WordPressで天気予報を追加する方法や、WordPressのエキスパートが選ぶベストクイズプラグインもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

37件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Dennis Muthomi says

    hi, I got a question for the time display:- what if my website has visitors from different time zones? will the date and time displayed be adjusted to their local time, or will it always show the time based on the website’s settings?

  3. Mrteesurez says

    Thanks for sharing.
    The time is not showing, only the date is showed. I want to show both date and time beside each other on the top for all visitorsb to see including the admins.

    • WPBeginner Support says

      For that you would need to change the format in the shortcode to: format=’F j, Y h:i’

      管理者

  4. Bert Hennephof says

    Code doesn’t work anymore after upgrade from PHP 7.4 to PHP 8.2
    WPCode indicates error in line:
    if ($atts[‘format’] == ”) {

    • WPBeginner Support says

      From testing the snippet on our end it currently is working in php 8.2, it would depend on the specific error message you are seeing on the line.

      管理者

  5. Ihtisham Z says

    Thank you for the great article.

    Just wanted to note here that, if the format attribute is not provided by the user then the shortcode will fail resulting in a critical error (parsing the attribute as string) and breaking the page. So, we need to convert the string to an array.

    function wpb_date_today($atts, $content = null) {
    if ( ! is_array( $atts ) ) {
    $atts = [];
    }

    // rest of the code…
    }

    • Chris Colotti says

      Thanks for the code update!!! Snippet had been working for some time then broke as you indicated..

  6. Generosus says

    Useful Tip:

    Method 2 (listed above) yields a date output in UTC format. To change the output to Local Time format, replace “date” with “wp_date” (2 places).

    It would be great to make a notation in your above instructions.

    You’re welcome :)

  7. Kathy says

    I really wanted to use this, however, after adding it to my functions file, I get this:
    Warning: Illegal string offset ‘format’ in /XXXXXXXXXX/functions.php

    It showed the date, however, the warning showed up too.

    • WPBeginner Support says

      Thank you for letting us know, we will look into this and for the time being the second shortcode with the format specified will avoid that warning.

      管理者

  8. hugo says

    Hi, thank you for the code.

    It’s possible to show the month in spanish and/or in number?

    greetings,

    • WPBeginner Support says

      For adding the date as numbers if you are using the echo date method, you would change F j, Y to m/d/Y

      管理者

  9. Rebekah says

    Is it possible to display a moving date? For instance, today’s date plus 7 days? I’d like to have something on my site that always displays the date one week from today.

  10. Henry says

    Adding this php code in the header file is OK but how can I control where I want to appear my date on the page?

  11. Jon says

    Or with Javascript:

    var dateToday = new Date(); var yearToday = dateToday.getFullYear(); document.write(yearToday);

  12. Clare says

    I’m beginning to discover that you can do anything with WP if you just take the time out to learn how – and most of it is so simple. 2011 is the year to ditch plugins and take the weight off my site!

    • Editorial Staff says

      We mentioned two solutions. One where you can use the server’s setting, or two where you can use WordPress setting. If you have a third solution, then please share it with us rather than saying that we are wrong because both solutions above work.

      管理者

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。