!
也想出现在这里? 联系我们
广告位
当前位置:首页>开发>WordPress开发手册>WordPress函数手册>WordPress函数文档get_ancestors()

WordPress函数文档get_ancestors()

获取当前分类/页面/链接的父级节点信息 描述 Returns an array containing the …--由S9社区整理

获取当前分类/页面/链接的父级节点信息

描述

Returns an array containing the parents of the given object.

用法

 <?php get_ancestors( $object_id, $object_type ); ?> 

Default Usage

 <?php $ancestors = get_ancestors( 6, 'page' ); ?> 

参数

$object_id

(int or string) (必填) The ID of the child object

默认值: None

$object_type

(string) (必填) The name of the object type (page, hierarchical post type, category, or hierarchical taxonomy) in question

默认值: None

返回值

(array) 

Array of ancestors from lowest to highest in the hierarchy

示例

Given the following category hierarchy (with IDs):

Books (6)

Fiction (23)

Mystery (208)

1
2
3
4

/* ———————————-
* wordpress函数 kim收集
* ———————————- */

<?php get_ancestors( 208, ‘category’ ); ?>

returns:

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

Given the a page hierarchy (with IDs):

About (447)

Child Page (448)

1
2
3
4

/* ———————————-
* wordpress函数 kim收集
* ———————————- */

<?php get_ancestors( 448, ‘page’ ); ?>

returns:

  Array
(
    [0] => 447

注意

Filter: get_ancestors is applied to ancestors array before it is returned.

历史

添加于 版本: 3.1.0

源文件

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Get an array of ancestor IDs for a given object.
*
* @since 3.1.0
* @since 4.1.0 Introduced the `$resource_type` argument.
*
* @param int    $object_id     Optional. The ID of the object. Default 0.
* @param string $object_type   Optional. The type of object for which we’ll be retrieving
*                              ancestors. Accepts a post type or a taxonomy name. Default empty.
* @param string $resource_type Optional. Type of resource $object_type is. Accepts ‘post_type’
*                              or ‘taxonomy’. Default empty.
* @return array An array of ancestors from lowest to highest in the hierarchy.
*/

function get_ancestors( $object_id = 0, $object_type = ”, $resource_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, $resource_type );

}

if ( ! $resource_type ) {

if ( is_taxonomy_hierarchical( $object_type ) ) {

$resource_type = ‘taxonomy’;

} elseif ( post_type_exists( $object_type ) ) {

$resource_type = ‘post_type’;

}

}

if ( ‘taxonomy’ === $resource_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’ === $resource_type ) {

$ancestors = get_post_ancestors($object_id);

}

/**

* Filter a given object’s ancestors.
*
* @since 3.1.0
* @since 4.1.1 Introduced the `$resource_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 $resource_type Type of resource $object_type is.
*/

return apply_filters( ‘get_ancestors’, $ancestors, $object_id, $object_type, $resource_type );

}

相关

Page Tags: get_all_page_ids(),
get_ancestors(),
get_page(),
get_page_link(),
get_page_by_path(),
get_page_by_title(),
get_page_children(),
get_page_hierarchy(),
get_page_uri(),
get_pages(),
is_page(),
page_uri_index(),
wp_list_pages(),
wp_page_menu()

原文:http://codex.wordpress.org/Function_Reference/get_ancestors

给TA打赏
共{{data.count}}人
人已打赏
WordPress函数手册

WordPress函数文档generic_ping()

2023-6-16 19:40:26

WordPress函数手册

WordPress函数文档fix_import_form_size()

2023-6-16 19:41:06

声明 本站上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。无意侵害您的权益,请发送邮件至 [email protected] 或点击右侧 私信:林沐阳 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索