get_ancestors()

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

描述

返回一个包含给定对象的父项的数组。

用法

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

默认用法

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

参数

$object_id  $object_id

(int or string) (必填) The ID of the child object
(整数或字符串)(必填)子对象的 ID

默认值: None

$object_type  $object_类型

(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):
给定以下类别层次结构(带 ID):

Books (6) 书籍 (6)

Fiction (23) 小说 (23)

Mystery (208)

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

返回

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

给定页面层次结构(带 ID):

About (447) 关于 (447)

Child Page (448) 儿童页面 (448)

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

返回

  Array
(
    [0] => 447
) 

注意

Filter: get_ancestors is applied to ancestors array before it is returned.
过滤器:get_ancestors在返回之前应用于祖先数组。

历史

添加于 版本: 3.1.0

源文件

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

/**
* Get an array of ancestor IDs for a given object.
* 获取给定对象的祖先 ID 数组。
*
* @since 3.1.0 * @since 3.1.0
* @since 4.1.0 Introduced the `$resource_type` argument.
* @since 4.1.0 引入了 '$resource_type' 参数。
*
* @param int    $object_id     Optional. The ID of the object. Default 0.
* @param int $object_id 可选。对象的 ID。默认值 0。
* @param string $object_type   Optional. The type of object for which we’ll be retrieving
* @param字符串$object_类型 可选。我们将检索的对象类型
*                              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’
* @param字符串 $resource_type 可选。资源的类型 $object_type 是。接受“post_type”
*                              or ‘taxonomy’. Default empty.
* 或“分类法”。默认值为空。
* @return array An array of ancestors from lowest to highest in the hierarchy.
* @return数组 层次结构中从最低到高的祖先数组。
*/

function get_ancestors( $object_id = 0, $object_type = ”, $resource_type = ” ) {
函数 get_ancestors( $object_id = 0, $object_type = “, $resource_type = ” ) {

$object_id = (int) $object_id;
$object_id = (int) $object_id;

$ancestors = array(); $ancestors = 数组();

if ( empty( $object_id ) ) {
if ( empty( $object_id ) ) {

/** This filter is documented in wp-includes/taxonomy.php */
/** 此过滤器记录在 wp-include/taxonomy 中.php */

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

}

if ( ! $resource_type ) {
if ( ! $resource_type ) {

if ( is_taxonomy_hierarchical( $object_type ) ) {
if ( is_taxonomy_hierarchical( $object_type ) ) {

$resource_type = ‘taxonomy’;
$resource_type = '分类法';

} elseif ( post_type_exists( $object_type ) ) {
} elseif ( post_type_exists( $object_type ) ) {

$resource_type = ‘post_type’;
$resource_type = 'post_type';

}

}

if ( ‘taxonomy’ === $resource_type ) {
if ( 'taxonomy' === $resource_type ) {

$term = get_term($object_id, $object_type);
$term = get_term($object_id, $object_type);

while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
而 ( ! is_wp_error($term) &&! 空( $term->父 ) &&! in_array( $term->父,$ancestors ) ) {

$ancestors[] = (int) $term->parent;
$ancestors[] = (整数) $term->父级;

$term = get_term($term->parent, $object_type);
$term = get_term($term->parent, $object_type);

}

} elseif ( ‘post_type’ === $resource_type ) {
} elseif ( 'post_type' === $resource_type ) {

$ancestors = get_post_ancestors($object_id);
$ancestors = get_post_ancestors($object_id);

}

/**

* Filter a given object’s ancestors.
* 过滤给定对象的祖先。
*
* @since 3.1.0 * @since 3.1.0
* @since 4.1.1 Introduced the `$resource_type` parameter.
* @since 4.1.1 引入了 '$resource_type' 参数。
*
* @param array  $ancestors     An array of object ancestors.
* @param数组$ancestors对象祖先的数组。
* @param int    $object_id     Object ID.
* @param int $object_id 对象 ID。
* @param string $object_type   Type of object.
* @param字符串 $object_type 对象的类型。
* @param string $resource_type Type of resource $object_type is.
* @param字符串 $resource_type 资源类型 $object_type 是。
*/

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

}

相关

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

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

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索