WordPress.org

Make WordPress Core

Ticket #17069: category-template.patch

File category-template.patch, 1.6 KB (added by rafaehlers, 7 years ago)

the patch with the new function

  • category-template.php

     
    6363}
    6464
    6565/**
     66 * Retrieve term parents with separator.
     67 *
     68 * @since
     69 *
     70 * @param int $id Category ID.
     71 * @param string $taxonomy Taxonomy name.
     72 * @param bool $link Optional, default is false. Whether to format with link.
     73 * @param string $separator Optional, default is '/'. How to separate categories.
     74 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
     75 * @param array $visited Optional. Already linked to categories to prevent duplicates.
     76 * @return string
     77 */
     78function get_term_parents( $id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
     79        $chain = '';
     80        $parent = &get_term( $id, $taxonomy );
     81        if ( is_wp_error( $parent ) )
     82                        return $parent;
     83
     84        if ( $nicename )
     85                        $name = $parent->slug;
     86        else
     87                        $name = $parent->name;
     88
     89        if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
     90                        $visited[] = $parent->parent;
     91                        $chain .= get_term_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
     92        }
     93
     94        if ( $link )
     95                        $chain .= '<a href="' . get_term_link( $parent->slug, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
     96        else
     97                        $chain .= $name.$separator;
     98        return $chain;
     99}
     100
     101/**
    66102 * Retrieve post categories.
    67103 *
    68104 * @since 0.71