WordPress.org

Make WordPress Core

Ticket #7030: 7030.get_ancestors.diff

File 7030.get_ancestors.diff, 2.2 KB (added by scribu, 8 years ago)

Use get_ancestors() in get_category_parents

  • wp-includes/category-template.php

     
    4747 * @param bool $link Optional, default is false. Whether to format with link.
    4848 * @param string $separator Optional, default is '/'. How to separate categories.
    4949 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
    50  * @param array $visited Optional. Already linked to categories to prevent duplicates.
     50 * @param array $deprecated Not used.
    5151 * @return string
    5252 */
    53 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
    54         $chain = '';
    55         $parent = &get_category( $id );
     53function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) {
     54        if ( !empty( $deprecated ) )
     55                _deprecated_argument( __FUNCTION__, '3.1' );
     56
     57        $parent = &get_term( $id, 'category' );
    5658        if ( is_wp_error( $parent ) )
    5759                return $parent;
    5860
    59         if ( $nicename )
    60                 $name = $parent->slug;
    61         else
    62                 $name = $parent->cat_name;
     61        $ancestors = get_ancestors( $parent->term_id, 'category' );
     62        array_unshift( $ancestors, $parent->term_id );
    6363
    64         if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
    65                 $visited[] = $parent->parent;
    66                 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
     64        $chain = '';
     65        $field = $nicename ? 'slug' : 'name';
     66        foreach ( array_reverse( $ancestors ) as $term_id ) {
     67                $term = get_term( $term_id, 'category' );
     68                $name = $term->$field;
     69
     70                if ( $link )
     71                        $chain .= '<a href="' . get_category_link( $term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $term->name ) ) . '">'.$name.'</a>' . $separator;
     72                else
     73                $chain .= $name.$separator;
    6774        }
    6875
    69         if ( $link )
    70                 $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->cat_name ) ) . '">'.$name.'</a>' . $separator;
    71         else
    72                 $chain .= $name.$separator;
    7376        return $chain;
    7477}
    7578
     
    10061009        return $r;
    10071010}
    10081011
    1009 ?>
    1010  No newline at end of file
     1012?>