| | 1499 | * Return a hierarchical array of a given term's parents. |
| | 1500 | * |
| | 1501 | * @since 4.1.0 |
| | 1502 | * |
| | 1503 | * @param string $term_id ID of Term to get parents. |
| | 1504 | * @param string $taxonomy Taxonomy name. |
| | 1505 | * @param boolean $reverse Optional, default to false. Whether to return terms array in reverse order. |
| | 1506 | * @return array|WP_Error List of Term IDs. WP_Error returned if $taxonomy does not exist or $taxonomy is not hierarchical. |
| | 1507 | */ |
| | 1508 | function get_term_parents( $term_id, $taxonomy, $reverse = false ) { |
| | 1509 | |
| | 1510 | if ( ! taxonomy_exists( $taxonomy ) ) { |
| | 1511 | return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); |
| | 1512 | } |
| | 1513 | |
| | 1514 | if( ! is_taxonomy_hierarchical( $taxonomy ) ) { |
| | 1515 | return new WP_Error( 'taxonomy_not_hierarchical', sprintf( __( 'The "%s" taxonomy is not hierarchical.' ), $taxonomy ) ); |
| | 1516 | } |
| | 1517 | |
| | 1518 | $term_id = intval( $term_id ); |
| | 1519 | |
| | 1520 | $term = get_term( $term_id, $taxonomy ); |
| | 1521 | |
| | 1522 | if ( is_wp_error( $term ) ) { |
| | 1523 | return $term; |
| | 1524 | } |
| | 1525 | |
| | 1526 | $parents = array(); |
| | 1527 | while( ! is_wp_error( $term ) ) { |
| | 1528 | if ( $term->parent && ( $term->parent != $term->term_id ) ) { |
| | 1529 | $parents[] = $term->parent; |
| | 1530 | $term = get_term( $term->parent, $taxonomy ); |
| | 1531 | } else { |
| | 1532 | break; |
| | 1533 | } |
| | 1534 | } |
| | 1535 | |
| | 1536 | if( $reverse ) { |
| | 1537 | $parents = array_reverse( $parents ); |
| | 1538 | } |
| | 1539 | |
| | 1540 | return $parents; |
| | 1541 | } |
| | 1542 | |
| | 1543 | /** |
| | 1544 | * Return term parents with a given separator. |
| | 1545 | * |
| | 1546 | * @since 4.1.0 |
| | 1547 | * |
| | 1548 | * @param string $term_id ID of Term to get parents. |
| | 1549 | * @param string $taxonomy Optional, default is 'category. Taxonomy name. |
| | 1550 | * @param bool $link Optional, default is false. Whether to format with link. |
| | 1551 | * @param string $separator Optional, default is '/'. How to separate categories. |
| | 1552 | * @param bool $nicename Optional, default is false. Whether to use nice name for display. |
| | 1553 | * @return string A list of term parents on success, empty string on failure. |
| | 1554 | */ |
| | 1555 | function get_term_parents_html( $term_id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false ) { |
| | 1556 | |
| | 1557 | $parents = (array) get_term_parents( $term_id, $taxonomy, true ); |
| | 1558 | |
| | 1559 | $chain = ''; |
| | 1560 | |
| | 1561 | if( 0 == count( $parents ) ) { |
| | 1562 | return $chain; |
| | 1563 | } |
| | 1564 | |
| | 1565 | foreach( $parents as $parent ) { |
| | 1566 | |
| | 1567 | $term = get_term( $parent, $taxonomy ); |
| | 1568 | |
| | 1569 | if ( is_wp_error( $term ) ) { |
| | 1570 | continue; |
| | 1571 | } |
| | 1572 | |
| | 1573 | if ( $nicename ) { |
| | 1574 | $name = $term->slug; |
| | 1575 | } else { |
| | 1576 | $name = $term->name; |
| | 1577 | } |
| | 1578 | |
| | 1579 | if( $chain ) { |
| | 1580 | $chain .= $separator; |
| | 1581 | } |
| | 1582 | |
| | 1583 | if ( $link ) { |
| | 1584 | $chain .= '<a href="' . esc_url( get_term_link( $term->term_id, $taxonomy ) ) . '">' . $name . '</a>'; |
| | 1585 | } else { |
| | 1586 | $chain .= $name; |
| | 1587 | } |
| | 1588 | |
| | 1589 | } |
| | 1590 | |
| | 1591 | return $chain; |
| | 1592 | } |
| | 1593 | |
| | 1594 | /** |