get_object_taxonomies()获取文章所在的自定义分类

目录

描述

Returns all the taxonomies for a post type or post object

get_object_taxonomies() 描述

用法

<?php get_object_taxonomies$object$output ); ?>

get_object_taxonomies() 用法

参数

$object

(array|string|object) (必填) Name of the post type, or a post object (row from posts)

默认值: None

$output

(string) (可选) The type of output to return, either taxonomy 'names' or 'objects'.

默认值: 'names'

get_object_taxonomies() 参数

返回值

(array) 

All taxonomy names or objects for the given post type/post object

get_object_taxonomies() 返回值

示例

Taxonomy names for post type

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php 

   $taxonomy_names = get_object_taxonomies( 'post' );
   print_r( $taxonomy_names);

?>

will typically output:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */


Array
(
    [0] => category
    [1] => post_tag
    [2] => post_format
)

Taxonomy objects for post type

If the $output parameter is 'objects', taxonomy objects will be returned as described in get_taxonomies()

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php 

   $taxonomy_objects = get_object_taxonomies( 'post', 'objects' );
   print_r( $taxonomy_objects);

?>

will output

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

Array
(
    [category] => stdClass Object
        (
            [hierarchical] => 1
            [update_count_callback] => 
            [rewrite] => 
            [query_var] => category_name
            [public] => 1
            [show_ui] => 1
            [show_tagcloud] => 1
            [_builtin] => 1
            [labels] => stdClass Object
                (
                    ...
                )

            ...

            [name] => category
            [label] => Categories
        )

    [post_tag] => stdClass Object
        (
            ...
        )

    [post_format] => stdClass Object
        (
            ....
        )

)

Taxonomy names for post object

To get the taxonomies for the current post, the current post object can be passed instead of the post type

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php 

    function get_current_post_taxonomies(){
        global $post;

        $taxonomy_names = get_object_taxonomies( $post );
        print_r( $taxonomy_names );
    }

    add_action('wp_head','get_current_post_taxonomies');

?>

will output

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */


Array
(
    [0] => category
    [1] => post_tag
    [2] => post_format
)

get_object_taxonomies() 示例

历史

添加于 版本: 2.3.0

get_object_taxonomies() 历史

源文件

get_object_taxonomies() 函数的代码位于 wp-includes/taxonomy.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Return all of the taxonomy names that are of $object_type.
 *
 * It appears that this function can be used to find all of the names inside of
 * $wp_taxonomies global variable.
 *
 * `` Should
 * result in `Array( 'category', 'post_tag' )`
 *
 * @since 2.3.0
 *
 * @global array $wp_taxonomies The registered taxonomies.
 *
 * @param array|string|WP_Post $object Name of the type of taxonomy object, or an object (row from posts)
 * @param string               $output Optional. The type of output to return in the array. Accepts either
 *                                     taxonomy 'names' or 'objects'. Default 'names'.
 * @return array The names of all taxonomy of $object_type.
 */
function get_object_taxonomies( $object, $output = 'names' ) {
	global $wp_taxonomies;

	if ( is_object($object) ) {
		if ( $object->post_type == 'attachment' )
			return get_attachment_taxonomies($object);
		$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;
}

get_object_taxonomies() 源文件

相关