get_the_terms

Sponsored Link

記事に所属しているタクソノミーのタームを取得します。

Sponsored Link

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

get_the_termsのファンクションタグ使用方法

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

get_the_termsのパラメーター

$id

(int) (required)記事ID。
デフォルト:0

(int) (required) Post ID
Default: 0

$taxonomy

(string) (required)タームから取得するためのタクソノミーの名前。例えば:’category’, ‘post_tag’, ‘taxonomy slug’
デフォルト:なし

(string) (required) Name of taxonomy to retrieve terms from. For example: ‘category’, ‘post_tag’, ‘taxonomy slug’
Default: None

戻り値

(array|false|wp_error)

成功したタームオブジェクトの配列。タームがなく失敗の場合、タクソノミーを取得し、無効なタクソノミーが入力されている場合はwp_errorオブジェクトを見つけます。
各タームオブジェクトは次のフィールドが含まれます。

Array of term objects on success. False if no terms are found in the given taxonomy and a wp_error object if an invalid taxonomy is entered.
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] =>
)

基本的な例

タームのリストをエコーします(ドラフトで呼び出されたタクソノミー)。これはget_the_term_listからの出力に似ていますが、タームはハイパーリンクされません:

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 &amp;&amp; ! 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; ?>

全てのカスタムタクソノミーにタームを取得します

テーマのfunctions.phpにこの関数を置きます。

place this function in theme functions.php

<?php // タクソノミーのタームリンクを取得
function custom_taxonomies_terms_links() {
	global $post, $post_id;
	// 記事IDで投稿を取得
	$post = &amp;get_post($post->ID);
	// 記事で投稿タイプを取得
	$post_type = $post->post_type;
	// 投稿タイプのタクソノミーを取得
	$taxonomies = get_object_taxonomies($post_type);
	foreach ($taxonomies as $taxonomy) {
		// 投稿するに関連するタームを取得
		$terms = get_the_terms( $post->ID, $taxonomy );
		if ( !empty( $terms ) ) {
			$out = array();
			foreach ( $terms as $term )
				$out[] = '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a>';
			$return = join( ', ', $out );
		}
	}
	return $return;
} ?>

今、あなたは投稿タイプに何も入力することなく、あなたのテーマ内にこの関数を使用することができます。

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(); ?>

注意

結果を取得するためにget_object_term_cache() あるいはwp_get_object_terms()を使用します。

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

変更ログ

Since: 2.5.0

ソースファイル

get_the_terms()は、wp-includes/category-template.php内に位置しています。

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

関連ファンクションタグ

Terms: 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(), wp_get_object_terms(), wp_set_object_terms(), wp_get_post_terms(), wp_set_post_terms()

Sponsored Link