get_the_terms

函数
get_the_terms ( $post, $taxonomy )
参数
  • (int|WP_Post) $post Post ID or object.
    Required:
  • (string) $taxonomy Taxonomy name.
    Required:
返回值
  • (WP_Term[]|false|WP_Error) Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.
定义位置
相关方法
get_the_term_listthe_termsget_termsget_the_tagsget_the_title_rss
引入
2.5.0
弃用
-

get_the_terms: 该函数返回当前文章或作为参数传递给它的文章的特定分类法的术语对象的数组。

检索附着在文章上的分类法术语。

function get_the_terms( $post, $taxonomy ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$terms = get_object_term_cache( $post->ID, $taxonomy );

	if ( false === $terms ) {
		$terms = wp_get_object_terms( $post->ID, $taxonomy );
		if ( ! is_wp_error( $terms ) ) {
			$term_ids = wp_list_pluck( $terms, 'term_id' );
			wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
		}
	}

	/**
	 * Filters the list of terms attached to the given post.
	 *
	 * @since 3.1.0
	 *
	 * @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
	 * @param int                $post_id  Post ID.
	 * @param string             $taxonomy Name of the taxonomy.
	 */
	$terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );

	if ( empty( $terms ) ) {
		return false;
	}

	return $terms;
}

常见问题

FAQs
查看更多 >