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で現在のタクソノミータイトル、URLなどを表示する方法

直感的なナビゲーション構造を作成し、カテゴリー、タグ、カスタムタクソノミーを使用して、サイト上のユーザーエンゲージメントを向上させることができます。 しかし、ユーザーエクスペリエンスにおいて重要な要素は、各タクソノミーアーカイブページ内で明確なコンテキストを提供することだと考えています。

WordPressのアーカイブページに、現在のタクソノミータイトルやURLなどの関連情報を直接表示することで、このコンテキストをプロバイダーに提供することができます。

この投稿では、WordPressで現在のタクソノミータイトルやURLなどを表示することで、ユーザーがサイト階層における自分の位置を理解しやすくする方法を紹介します。

Display current taxonomy title, URL, and more in WordPress theme

WordPressテーマでタクソノミーアーカイブテンプレートを作成する

WordPressテーマ開発を学んだり、独自のカスタムWordPressテーマを作ったりしている場合、カテゴリーやタグなどのタクソノミーページ、またはサイトにあるカスタムタクソノミー用のカスタムテンプレートを作りたいと思うかもしれません。

WordPressには強力なテンプレートエンジンが搭載されています。これによって、WordPressサイトのさまざまな部分のカスタマイザーテンプレートをテーマで簡単に作成することができます。

例えば、テーマにcategory.phpテンプレートを作成するだけで、WordPressはそのテンプレートを使ってカテゴリー・アーカイブページを表示します。

Example of a taxonomy template used in a WordPress theme

同様に、taxonomy-{タクソノミー}-{キーワード}.php形式で名前を付けることで、任意のカスタムタクソノミーのテンプレートを作成することができます。詳しくは、WordPressテンプレート階層チートシート(初心者向け)をご覧ください。

タクソノミー・テンプレート・ファイルを作成したら、テーマのarchive.phpテンプレート・コードをコピー&ペーストしてください。

しかし、それでは非常に一般的です。タクソノミーページに特化した方がいいかもしれない。

例えば、タクソノミーのタイトルを別の場所に表示したり、タクソノミーのRSSフィードへのリンクを追加したりすることができます。また、タクソノミーの説明を表示したり、投稿数を表示したりすることもできます。

ということで、WordPressでタクソノミー関連のデータを取得し、WordPressテーマに表示する方法を見ていきましょう。

WordPressでタクソノミーのタイトルやURLなどを表示する

タクソノミー関連のデータをすべて表示するには、どのタクソノミーページが表示されているかを動的に把握し、その特定のタクソノミーキーワードに必要なデータをすべて取得する必要があります。

まず、タクソノミー・テンプレートに以下のコードをコピー&ペーストする必要があります:

<?php $term = get_queried_object();  ?>

これは、現在いるページに基づいて現在のタクソノミーの情報を取得します。

例えば、「ビジネス」というカテゴリーページにいた場合、そのタクソノミー・タームの情報を取得する。

このコードの後に、タクソノミーのタイトルやその他の情報をこのように表示することができます:

echo $term->name; // will show the name
echo $term->taxonomy; // will show the taxonomy
echo $term->slug; // will show taxonomy slug

以下の値のいずれかを使って同じことができる:

  • タームID
  • 名称
  • スラッグ
  • 用語グループ
  • term_taxonomy_id
  • タクソノミー
  • 説明
  • カウント
  • フィルター
  • メタ情報

実際の例を見てみましょう。テスト用の子テーマでは、キーワードのタイトル、タクソノミー名、投稿記事数、用語の説明を表示したいと考えました。

このデータを表示するために以下のコードを使用した:

<?php $term = get_queried_object();  ?>
 
<h1 class="category-title"><?php echo $term->name; ?><span class="taxonomy-label"><?php echo $term->taxonomy; ?> (<?php echo $term->count; ?> articles)</span></h1>
 
<p class="category-description"><?php echo $term->description; ?></p>

CSSクラスをいくつか追加したので、カスタムCSSを追加してテキストのスタイルも設定できることがわかるだろう。

これがテストサイトでの様子だ:

Taxonomy template

WordPressタクソノミーのエキスパートガイド

WordPressテーマで現在のタクソノミーのタイトルやURLなどを表示する方法はお分かりいただけたと思いますが、WordPressのタクソノミーに関連する他のガイドもご覧ください:

この投稿が、WordPressテーマで現在のタクソノミーのタイトルやURLなどを表示する方法を学ぶのにお役に立てば幸いです。また、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$編集プロセスをご覧ください。

アバター

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

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

  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. Pete says

    This works well too…
    $queried_object = get_queried_object();
    $this_tax = get_taxonomy( $queried_object->taxonomy );
    echo $this_tax->labels->singular_name; //change this accordingly

  3. Keyur says

    Many thanks for this code – this helped me to resolve my problem for last one week. I was trying it myself but didn’t managed but using your code, I got it.
    Thanks once again.

  4. sami says

    What if we want to get Taxonomy -> Terms name/title outside the loop with wp Query on Page template? Plus how to get Taxonomy name/title with the same scenario.

    Thanks

  5. Kalle Pedersen says

    Noob question here: where is the “archive page” in which I need to paste the above code? Have tried it in different pages, but to no avail :-(

  6. Nechemya K says

    Hi.
    I trying to build a website for movies.
    I have a few post types like movies, tv, and more
    And I have a few taxonomies like: directors. Stars. Year. And more.

    How can I make taxonomy pages to show only the movies post type title in the page?
    Because when I ask to show all post in this tax, all of the post from all types shown

    Thanks for you help.

  7. Aaron says

    The title and first paragraph promises directions on how to get the URL, yet I see no mention of how to do so in the article body.

  8. Saad says

    You Made My Day I was looking for this for so long thank you guys very much :). it will really help me in building a new theme for my blog .

  9. Frank Pereiro says

    This is really, really nice.

    I wish there was more post on taxonomies like this one.

    Thank you very much for sharing

返信を残す

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