^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_ancestors()用法示例

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.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

$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


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()

  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
    )

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈