get_object_taxonomies

函式
get_object_taxonomies ( $object, $output = 'names' )
引數
  • (string|string[]|WP_Post) $object Name of the type of taxonomy object, or an object (row from posts)
    Required:
  • (string) $output Optional. The type of output to return in the array. Accepts either 'names' or 'objects'. Default 'names'.
    Required:
    Default: 'names'
返回值
  • (string[]|WP_Taxonomy[]) The names or objects of all taxonomies of `$object_type`.
定義位置
相關方法
get_post_taxonomiesget_the_taxonomiesget_taxonomiesget_attachment_taxonomiesthe_taxonomies
引入
2.3.0
棄用
-

get_object_taxonomies函式用來檢索與一個給定的文章型別或其他物件型別相關的分類學物件的列表: 這個函式可以用來檢索一個自定義文章型別的分類學資料,或者檢索當前文章或頁面的分類學資料。

返回為所請求的物件或物件型別註冊的分類法的名稱或物件。
如文章物件或文章型別的名稱。

例子:

$taxonomies = get_object_taxonomies( ‘post’ );

結果:

Array( ‘category’, ‘post_tag’ )

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_object_taxonomies( $object, $output = 'names' ) {
global $wp_taxonomies;
if ( is_object( $object ) ) {
if ( 'attachment' === $object->post_type ) {
return get_attachment_taxonomies( $object, $output );
}
$object = $object->post_type;
}
$object = (array) $object;
$taxonomies = array();
foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
if ( 'names' === $output ) {
$taxonomies[] = $tax_name;
} else {
$taxonomies[ $tax_name ] = $tax_obj;
}
}
}
return $taxonomies;
}
function get_object_taxonomies( $object, $output = 'names' ) { global $wp_taxonomies; if ( is_object( $object ) ) { if ( 'attachment' === $object->post_type ) { return get_attachment_taxonomies( $object, $output ); } $object = $object->post_type; } $object = (array) $object; $taxonomies = array(); foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) { if ( array_intersect( $object, (array) $tax_obj->object_type ) ) { if ( 'names' === $output ) { $taxonomies[] = $tax_name; } else { $taxonomies[ $tax_name ] = $tax_obj; } } } return $taxonomies; }
function get_object_taxonomies( $object, $output = 'names' ) {
	global $wp_taxonomies;

	if ( is_object( $object ) ) {
		if ( 'attachment' === $object->post_type ) {
			return get_attachment_taxonomies( $object, $output );
		}
		$object = $object->post_type;
	}

	$object = (array) $object;

	$taxonomies = array();
	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
		if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
			if ( 'names' === $output ) {
				$taxonomies[] = $tax_name;
			} else {
				$taxonomies[ $tax_name ] = $tax_obj;
			}
		}
	}

	return $taxonomies;
}

常見問題

FAQs
檢視更多 >