get_ancestors() - WordPress 函数参考中文文档

返回介绍

get_ancestors()

发布于 2017-09-10 字数4460 浏览 962 评论 0

get_ancestors( int $object_id,  string $object_type = '',  string $re源代码_type = '' )

Get an array of ancestor IDs for a given object.


描述


参数

$object_id

(int)
(Optional)
The ID of the object. Default 0.

$object_type

(string)
(Optional)
The type of object for which we’ll be retrieving ancestors. Accepts a post type or a taxonomy name.

Default value: ”

$re源代码_type

(string)
(Optional)
Type of re源代码 $object_type is. Accepts ‘post_type’ or ‘taxonomy’.

Default value: ”


返回值

(array) An array of ancestors from lowest to highest in the hierarchy.


源代码

File: wp-includes/taxonomy.php

function get_ancestors( $object_id = 0, $object_type = '', $re源代码_type = '' ) {
	$object_id = (int) $object_id;

	$ancestors = array();

	if ( empty( $object_id ) ) {

		/** This filter is documented in wp-includes/taxonomy.php */
		return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $re源代码_type );
	}

	if ( ! $re源代码_type ) {
		if ( is_taxonomy_hierarchical( $object_type ) ) {
			$re源代码_type = 'taxonomy';
		} elseif ( post_type_exists( $object_type ) ) {
			$re源代码_type = 'post_type';
		}
	}

	if ( 'taxonomy' === $re源代码_type ) {
		$term = get_term($object_id, $object_type);
		while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
			$ancestors[] = (int) $term->parent;
			$term = get_term($term->parent, $object_type);
		}
	} elseif ( 'post_type' === $re源代码_type ) {
		$ancestors = get_post_ancestors($object_id);
	}

	/**
	 * Filters a given object's ancestors.
	 *
	 * @since 3.1.0
	 * @since 4.1.1 Introduced the `$re源代码_type` parameter.
	 *
	 * @param array  $ancestors     An array of object ancestors.
	 * @param int    $object_id     Object ID.
	 * @param string $object_type   Type of object.
	 * @param string $re源代码_type Type of re源代码 $object_type is.
	 */
	return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $re源代码_type );
}

更新日志

Version 描述
4.1.0 Introduced the $re源代码_type argument.
3.1.0 Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php:
    get_ancestors
  • wp-includes/taxonomy.php:
    is_taxonomy_hierarchical()
  • wp-includes/taxonomy.php:
    get_term()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post.php:
    post_type_exists()
  • wp-includes/post.php:
    get_post_ancestors()
  • wp-includes/load.php:
    is_wp_error()
  • Show 2 more uses
    Hide more uses


Used By

  • wp-includes/category-template.php:
    get_term_parents_list()
  • wp-admin/includes/ajax-actions.php:
    wp_ajax_add_tag()
  • wp-includes/taxonomy.php:
    get_term_link()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Given the following category hierarchy (with IDs):

    – Books (6)
    — Fiction (23)
    — Mystery (208)

    
    <?php get_ancestors( 208, 'category' ); ?>
    

    returns:

    Array
    (
    [0] => 23
    [1] => 6
    )

  2. Given the a page hierarchy (with IDs):

    – About (447)
    — Child Page (448)

    
    <?php get_ancestors( 448, 'page' ); ?>
    

    returns:

    Array
    (
    [0] => 447
    )

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!