get_term

函式
get_term ( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' )
引數
  • (int|WP_Term|object) $term If integer, term data will be fetched from the database, or from the cache if available. If stdClass object (as in the results of a database query), will apply filters and return a `WP_Term` object with the `$term` data. If `WP_Term`, will return `$term`.
    Required:
  • (string) $taxonomy Optional. Taxonomy name that `$term` is part of.
    Required:
    Default: (empty)
  • (string) $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
    Required:
    Default: OBJECT
  • (string) $filter Optional. How to sanitize term fields. Default 'raw'.
    Required:
    Default: 'raw'
返回值
  • (WP_Term|array|WP_Error|null) WP_Term instance (or array) on success, depending on the `$output` value. WP_Error if `$taxonomy` does not exist. Null for miscellaneous failure.
相關
  • sanitize_term_field()
定義位置
相關方法
get_termsget_term_byget_themeget_themesget_footer
引入
2.3.0
棄用
-

get_term: 這個函式通過ID檢索一個特定的術語物件。它需要兩個引數:術語ID和該術語所屬的分類法名稱。它返回術語物件。

按術語ID從資料庫中獲取所有術語資料。

get_term函式的用途是對術語物件應用過濾器。在應用過濾器之前,可以從資料庫中獲得一個術語物件。

$term ID必須是$taxonomy的一部分,才能從資料庫中獲取。失敗,可能會被鉤子捕獲。失敗將是與$wpdb在get_row方法中返回的值相同。

有兩個鉤子,一個是專門針對每個術語的,名為’get_term’,第二個是針對分類學名稱的,’term_$taxonomy’。這兩個鉤子都獲得了術語物件和分類法名稱作為引數。兩個鉤子都期望返回一個術語物件。

{@see ‘get_term’}鉤子 – 接收兩個引數:術語物件和分類法名稱。必須返回術語物件。在get_term()中使用,作為每個$term的總過濾器。

{@see ‘get_$taxonomy’} 鉤子 – 接收兩個引數:術語物件和分類法名稱。必須返回術語物件。$taxonomy將是分類名稱,因此,例如,如果是’category’,它將是’get_category’作為過濾器的名稱。適用於用於自定義分類法或插入到預設分類法中。

function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
	if ( empty( $term ) ) {
		return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
	}

	if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
	}

	if ( $term instanceof WP_Term ) {
		$_term = $term;
	} elseif ( is_object( $term ) ) {
		if ( empty( $term->filter ) || 'raw' === $term->filter ) {
			$_term = sanitize_term( $term, $taxonomy, 'raw' );
			$_term = new WP_Term( $_term );
		} else {
			$_term = WP_Term::get_instance( $term->term_id );
		}
	} else {
		$_term = WP_Term::get_instance( $term, $taxonomy );
	}

	if ( is_wp_error( $_term ) ) {
		return $_term;
	} elseif ( ! $_term ) {
		return null;
	}

	// Ensure for filters that this is not empty.
	$taxonomy = $_term->taxonomy;

	/**
	 * Filters a taxonomy term object.
	 *
	 * The {@see 'get_$taxonomy'} hook is also available for targeting a specific
	 * taxonomy.
	 *
	 * @since 2.3.0
	 * @since 4.4.0 `$_term` is now a `WP_Term` object.
	 *
	 * @param WP_Term $_term    Term object.
	 * @param string  $taxonomy The taxonomy slug.
	 */
	$_term = apply_filters( 'get_term', $_term, $taxonomy );

	/**
	 * Filters a taxonomy term object.
	 *
	 * The dynamic portion of the hook name, `$taxonomy`, refers
	 * to the slug of the term's taxonomy.
	 *
	 * Possible hook names include:
	 *
	 *  - `get_category`
	 *  - `get_post_tag`
	 *
	 * @since 2.3.0
	 * @since 4.4.0 `$_term` is now a `WP_Term` object.
	 *
	 * @param WP_Term $_term    Term object.
	 * @param string  $taxonomy The taxonomy slug.
	 */
	$_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy );

	// Bail if a filter callback has changed the type of the `$_term` object.
	if ( ! ( $_term instanceof WP_Term ) ) {
		return $_term;
	}

	// Sanitize term, according to the specified filter.
	$_term->filter( $filter );

	if ( ARRAY_A === $output ) {
		return $_term->to_array();
	} elseif ( ARRAY_N === $output ) {
		return array_values( $_term->to_array() );
	}

	return $_term;
}

常見問題

FAQs
檢視更多 >