SYNCER

SYNCER

get_term_link() - タクソノミーのURLを取得する

公開日:

get_term_link()は、指定したタクソノミーのURLアドレスを取得する関数です。

構文

mixed get_term_link ( object|int|string $term, string $taxonomy = '' )

パラメータ

$term

対象のタクソノミーをタクソノミーオブジェクト、またはID、スラッグで指定する。

$taxonomy

初期値: ''

タクソノミーの種類を、名前で指定する。例えばカテゴリだったらcategory、タグだったらpost_tagなど。第1引数をオブジェクトで指定した場合は必要ない。

返り値

mixed

取得できた場合は、対象のタクソノミーのURLアドレスが文字列で返る。

取得できなかった場合は、WP_Errorオブジェクトが返る。

関数

この関数内で利用している、別のWordPress関数です。

  • apply_filters() - フィルターイベントを発火する。
  • foreach()
  • get_ancestors()
  • get_extra_permastruct()
  • get_taxonomy()
  • get_term()
  • get_term_by()
  • home_url() - 現在のブログのホームのURLを取得する。
  • newWP_Error()

フック

関数内で呼び出されるイベントです。

フィルター

  • tag_link
  • category_link
  • term_link

サンプルコード

カテゴリページのURLを取得

IDが4のカテゴリ(category)の、タクソノミーのURLを取得します。

php

<?php
	// 実行
	$result = get_term_link( 4, "category" ) ;

	// 結果
	var_dump( $result ) ;

結果 (出力内容) - PHP7.0.13

string(37) "https://wp.syncer.jp/category/animal/"

エラーの場合

引数に不備があって取得対象が存在しなかった場合、WP_Errorオブジェクトが返ります。

php

<?php
	// 実行
	$result = get_term_link( 9999999 ) ;

	// 結果
	var_dump( $result ) ;

結果 (出力内容) - PHP7.0.13

object(WP_Error)#336 (2) {
  ["errors"]=>
  array(1) {
    ["invalid_term"]=>
    array(1) {
      [0]=>
      string(21) "キーワードなし"
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

エラーの判定方法

この関数を利用する場合、エラーを考慮する必要があります。下記の通りエラー判定をして、取得できた場合だけ処理を続けて下さい。

php

<?php
	// タクソノミーのURLを取得する
	$result = get_term_link( 11 ) ;

	// 取得できなかった場合
	if ( is_wp_error( $result ) ) {
		// エラーの場合は処理をしない
		echo "URLを取得できませんでした。" ;

	// 取得できた場合
	} else {
		// $resultにはURLが入っている
		// ...

	}

結果 (出力内容) - PHP7.0.13

URLを取得できませんでした。

ソースコード

wp-includes/taxonomy.php

/**
 * Generate a permalink for a taxonomy term archive.
 *
 * @since 2.5.0
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param object|int|string $term     The term object, ID, or slug whose link will be retrieved.
 * @param string            $taxonomy Optional. Taxonomy. Default empty.
 * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist.
 */
function get_term_link( $term, $taxonomy = '' ) {
	global $wp_rewrite;

	if ( !is_object($term) ) {
		if ( is_int( $term ) ) {
			$term = get_term( $term, $taxonomy );
		} else {
			$term = get_term_by( 'slug', $term, $taxonomy );
		}
	}

	if ( !is_object($term) )
		$term = new WP_Error('invalid_term', __('Empty Term'));

	if ( is_wp_error( $term ) )
		return $term;

	$taxonomy = $term->taxonomy;

	$termlink = $wp_rewrite->get_extra_permastruct($taxonomy);

	$slug = $term->slug;
	$t = get_taxonomy($taxonomy);

	if ( empty($termlink) ) {
		if ( 'category' == $taxonomy )
			$termlink = '?cat=' . $term->term_id;
		elseif ( $t->query_var )
			$termlink = "?$t->query_var=$slug";
		else
			$termlink = "?taxonomy=$taxonomy&term=$slug";
		$termlink = home_url($termlink);
	} else {
		if ( $t->rewrite['hierarchical'] ) {
			$hierarchical_slugs = array();
			$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
			foreach ( (array)$ancestors as $ancestor ) {
				$ancestor_term = get_term($ancestor, $taxonomy);
				$hierarchical_slugs[] = $ancestor_term->slug;
			}
			$hierarchical_slugs = array_reverse($hierarchical_slugs);
			$hierarchical_slugs[] = $slug;
			$termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
		} else {
			$termlink = str_replace("%$taxonomy%", $slug, $termlink);
		}
		$termlink = home_url( user_trailingslashit($termlink, 'category') );
	}
	// Back Compat filters.
	if ( 'post_tag' == $taxonomy ) {

		/**
		 * Filters the tag link.
		 *
		 * @since 2.3.0
		 * @deprecated 2.5.0 Use 'term_link' instead.
		 *
		 * @param string $termlink Tag link URL.
		 * @param int    $term_id  Term ID.
		 */
		$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
	} elseif ( 'category' == $taxonomy ) {

		/**
		 * Filters the category link.
		 *
		 * @since 1.5.0
		 * @deprecated 2.5.0 Use 'term_link' instead.
		 *
		 * @param string $termlink Category link URL.
		 * @param int    $term_id  Term ID.
		 */
		$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
	}

	/**
	 * Filters the term link.
	 *
	 * @since 2.5.0
	 *
	 * @param string $termlink Term link URL.
	 * @param object $term     Term object.
	 * @param string $taxonomy Taxonomy slug.
	 */
	return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}

参考リンク