WordPress.org

Make WordPress Core

Ticket #30415: 30415.diff

File 30415.diff, 2.6 KB (added by hlashbrooke, 7 years ago)

Patch as described in ticket body.

  • wp-includes/category-template.php

     
    4040 * @return string|WP_Error A list of category parents on success, WP_Error on failure.
    4141 */
    4242function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
    43         $chain = '';
    44         $parent = get_term( $id, 'category' );
    45         if ( is_wp_error( $parent ) )
    46                 return $parent;
    47 
    48         if ( $nicename )
    49                 $name = $parent->slug;
    50         else
    51                 $name = $parent->name;
    52 
    53         if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
    54                 $visited[] = $parent->parent;
    55                 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
    56         }
    57 
    58         if ( $link )
    59                 $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
    60         else
    61                 $chain .= $name.$separator;
    62         return $chain;
     43        return get_term_parents( $id, $link, $separator, $nicename, $visited, 'category' );
    6344}
    6445
    6546/**
     
    14031384
    14041385        return $r;
    14051386}
     1387
     1388/**
     1389 * Retrieve term parents with separator.
     1390 *
     1391 * @since 4.1.0
     1392 *
     1393 * @param int $id Taxonomy term ID.
     1394 * @param bool $link Optional, default is false. Whether to format with link.
     1395 * @param string $separator Optional, default is '/'. How to separate categories.
     1396 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
     1397 * @param array $visited Optional. Already linked to categories to prevent duplicates.
     1398 * @param string $taxonomy Optional, default is 'category'. Which taxonomy that the term belongs to.
     1399 * @return string|WP_Error A list of category parents on success, WP_Error on failure.
     1400 */
     1401function get_term_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array(), $taxonomy = 'category' ) {
     1402
     1403        if( ! is_taxonomy_hierarchical( $taxonomy ) ) {
     1404                return false;
     1405        }
     1406
     1407        $chain = '';
     1408        $parent = get_term( $id, $taxonomy );
     1409        if ( is_wp_error( $parent ) ) {
     1410                return $parent;
     1411        }
     1412
     1413        if ( $nicename ) {
     1414                $name = $parent->slug;
     1415        } else {
     1416                $name = $parent->name;
     1417        }
     1418
     1419        if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
     1420                $visited[] = $parent->parent;
     1421                $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
     1422        }
     1423
     1424        if ( $link ) {
     1425                $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
     1426        } else {
     1427                $chain .= $name.$separator;
     1428        }
     1429
     1430        return $chain;
     1431}