最新公告
  • 欢迎您光临知事网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入我们
  • WordPress函数get_ancestors()用法 获取当前分类页面链接的父级节点信息

    描述:

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

    用法:

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

    参数:

    $object_id
    
    (int or string) (必填) 子分类ID
    
    默认值: None
    
    $object_type
    
    (string) (必填) 对象类型的名称(页、层次文章类型、类别或层次分类法)
    
    默认值: None

    示例:

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

    源文件:

    /**
    * 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 );
    }
    找互联网优质资源,上知事网!
    知事网 » WordPress函数get_ancestors()用法 获取当前分类页面链接的父级节点信息

    常见问题FAQ

    网盘提取密码在哪里?
    在网站右侧有个“文件密码”,点击复制即可!
    网站的积分如何获得?
    注册登录后,每天签到可获得2个积分,每天可签到一次!

    发表评论

    • 1231会员总数(位)
    • 665资源总数(个)
    • 21本周发布(个)
    • 0 今日发布(个)
    • 3457稳定运行(天)

    提供最优质的资源集合

    Mac软件 Dedecms源码
    升级SVIP尊享更多特权立即升级