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でカスタムフィールドをループ外に表示する方法

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

WordPressのループの外側にカスタムフィールドを表示したいですか?通常、カスタムフィールドは他の投稿コンテンツやメタデータと一緒にWordPressのループ内に表示されます。この投稿では、WordPressでカスタムフィールドをループの外側に表示する方法を紹介します。

How to display custom fields outside the WordPress loop

WordPressのカスタムフィールドとは?

カスタムフィールドは、WordPressの投稿に追加のメタ情報を追加し、投稿コンテンツと一緒に表示することができます。

表示オプションのカスタムフィールドメタボックスを有効化するだけで、カスタムフィールドを追加することができます。また、WordPressでカスタムメタボックスを作成することで、カスタムフィールドをより良いユーザーインターフェースにすることができます。

Adding custom field to a WordPress post or page

詳しくは、WordPressカスタムフィールドの使い方の初心者ガイドをご覧ください。

カスタムフィールドは投稿にメタデータを追加するので、他の投稿コンテンツと一緒にWordPressのループ内に簡単に表示することができます。しかし、ループの外側に表示したい場合もあります。例えば、サイドバーウィジェットに表示したい場合などです。このような場合、少し厄介になります。

ということで、WordPressでカスタムフィールドをループの外に簡単に表示する方法を見てみましょう。

WordPressでカスタムフィールドのデータをループの外に表示する

カスタムフィールドのメタ情報をループの外に表示する代わりに、メインループに影響を与えずにWordPressテーマで複数のループを使用する方法を実際に紹介します。

この投稿では、WordPressテーマファイルにコードを追加する必要があります。この作業を行ったことがない場合は、WordPressでコードをコピー&ペーストする方法のガイドを参照してください。

WordPressのカスタムフィールドデータを表示したいテーマファイルに以下のコードを追加する必要があります。

[cbk1]

このコードは、投稿IDを取得するためにグローバル変数$wp_queryをロードするだけです。その後、get_post_meta()関数を使用してカスタムフィールドのデータを取得して出力します。

Your-Custom-Fieldを実際のカスタムフィールドに変更することを忘れないでください。

あなたのニーズに一致するようにコードをカスタマイズできます。異なる投稿やページのカスタムフィールドのデータを取得して表示するために、他のクエリー引数を使用することもできます。

別の例を見てみましょう。これはWP_Queryクラスを使用したもので、WordPressテーマファイル内で複数のループを使用するための、より優れた柔軟な方法です。

カスタムフィールドを表示したいテーマや子テーマにこのコードを追加するだけです。

[cbk2]

Moodをあなた自身のカスタムフィールド名に、投稿IDをあなた自身の投稿またはページIDに置き換えることを忘れないでください。

Custom field in WordPress sidebar widget

これですべてです。

この投稿が、WordPressでカスタムフィールドをループの外に表示する方法を学ぶのに役立てば幸いです。初心者向けのWordPressテーマ・チートシートもご覧ください。

この投稿が気に入ったら、WordPress動画チュートリアルのYouTubeチャンネルを購読してください。Twitterや 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

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

  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. Daniel R says

    Hi there,

    Great article, I’ve used WPB a few times to help me work some things out!

    I’m currently trying to get the custom field information from the most recent post in a specific category, and to display this as inline text within a paragraph.

    Do you know if there is any plugin for this or if we can achieve this with PHP/JavaScript?

    Basically what I want to ask WordPress is “Go and get the most recent post in the todays-tip category then find the value of the custom field ‘odds’ and display ‘odds’ inside this span.’

    Really I’d like to stay away from hard coding this into the page and would prefer a shortcode/JS solution as the paragraph is editable. Basically one of the webmasters might go in and change the text in the paragraph but still want to show the ‘odds’ in a certain place. The page is built on a drag-n-drop editor on xPro.

    • Daniel R says

      I’m currently using a recent posts plugin shortcode to display the title in another paragraph. I then strip back all the styling to make the text inline with the paragraph. It’s a bit of a dodgy route!

返信を残す

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