当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

関数リファレンス/get the terms

提供: WordPress Codex 日本語版
< 関数リファレンス
2014年7月21日 (月) 22:17時点におけるGblsm (トーク | 投稿記録)による版 (en:Function Reference/get the terms 19:26, 15 April 2014 Sam 版を流し込み。)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

このページ「関数リファレンス/get the terms」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

Description

Retrieve the terms of the taxonomy that are attached to the post.

Usage

<?php get_the_terms( $id, $taxonomy ); ?>

Parameters

$id
int) (required) Post ID
初期値: 0
$taxonomy
string|array) (必須) Name of taxonomy to retrieve terms from. For example: 'category', 'post_tag', 'taxonomy slug'
初期値: なし

Returns

(array|false|wp_error) 
Array of term objects on success; false if the post contains no terms from the given taxonomy; false if the post doesn't exist; and a Class_Reference/WP_Error object if an invalid taxonomy is entered.

A post with no terms assigned gives a false result, not an empty array.

Each term object will contain the following fields:

stdClass Object
(
    [term_id] =>
    [name] =>
    [slug] =>
    [term_group] => 
    [term_order] => 
    [term_taxonomy_id] =>
    [taxonomy] =>
    [description] => 
    [parent] =>
    [count] =>
    [object_id] =>
)

Example

A Basic Example

Echoing the list of terms (for a taxonomy called on-draught). This is similar to the output from get_the_term_list, but without the terms being hyperlinked:

<?php
$terms = get_the_terms( $post->ID, 'on-draught' );
						
if ( $terms && ! is_wp_error( $terms ) ) : 

	$draught_links = array();

	foreach ( $terms as $term ) {
		$draught_links[] = $term->name;
	}
						
	$on_draught = join( ", ", $draught_links );
?>

<p class="beers draught">
	On draught: <span><?php echo $on_draught; ?></span>
</p>

<?php endif; ?>

Get terms for all custom taxonomies

place this function in theme functions.php

<?php
// get taxonomies terms links
function custom_taxonomies_terms_links(){
  // get post by post id
  $post = get_post( $post->ID );

  // get post type by post
  $post_type = $post->post_type;

  // get post type taxonomies
  $taxonomies = get_object_taxonomies( $post_type, 'objects' );

  $out = array();
  foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){

    // get the terms related to post
    $terms = get_the_terms( $post->ID, $taxonomy_slug );

    if ( !empty( $terms ) ) {
      $out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
      foreach ( $terms as $term ) {
        $out[] =
          '  <li><a href="'
        .    get_term_link( $term->slug, $taxonomy_slug ) .'">'
        .    $term->name
        . "</a></li>\n";
      }
      $out[] = "</ul>\n";
    }
  }

  return implode('', $out );
}
?>

Now you can use this function in your themes for your post types without the need to input anything

<?php echo custom_taxonomies_terms_links(); ?>

Notes

It uses get_object_term_cache() or wp_get_object_terms() to retrieve results.

Change Log

Source File

get_the_terms() is located in wp-includes/category-template.php.

Related

タームタグ: is_term(), term_exists(), get_objects_in_term(), get_term(), get_term_by(), get_term_children(), get_term_link(), get_terms(), get_the_terms(), get_the_term_list(), has_term(), sanitize_term(), the_terms(), get_object_taxonomies() is_object_in_taxonomy() the_taxonomies() wp_get_object_terms(), wp_set_object_terms(), wp_get_post_terms(), wp_set_post_terms(), wp_delete_object_term_relationships()



関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function Reference/get the terms最新版との差分