返回介绍

get_ancestors()

发布于 2017-09-10 22:43:29 字数 4413 浏览 1127 评论 0 收藏 0

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

Get an array of ancestor IDs for a given object.


description


参数

$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 );
}

更新日志

Versiondescription
4.1.0Introduced the $re源代码_type argument.
3.1.0Introduced.

相关函数

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
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文