WordPress.org

Make WordPress Core

Ticket #7030: 7030.diff

File 7030.diff, 2.8 KB (added by wojtek.szkutnik, 5 years ago)
  • wp-includes/category-template.php

     
    4646 * @param int $id Category ID. 
    4747 * @param bool $link Optional, default is false. Whether to format with link. 
    4848 * @param string $separator Optional, default is '/'. How to separate categories. 
    49  * @param bool $nicename Optional, default is false. Whether to use nice name for display. 
     49 * @param bool $useslug Optional, default is false. Whether to use nice name or a slug for display. 
    5050 * @param array $visited Optional. Already linked to categories to prevent duplicates. 
     51 * @param bool $trailing_separator Optional. Use trailing separator? Default true 
     52 * @param bool $use_desc_title Optional. Use description for title? Default false 
    5153 * @return string 
    5254 */ 
    53 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) { 
     55function get_category_parents( $id, $link = false, $separator = '/', $useslug = false, $visited = array(), $trailing_separator = true, $use_desc_title = false ) { 
    5456        $chain = ''; 
    5557        $parent = &get_category( $id ); 
    5658        if ( is_wp_error( $parent ) ) 
    5759                return $parent; 
    5860 
    59         if ( $nicename ) 
     61        if ( $useslug ) 
    6062                $name = $parent->slug; 
    6163        else 
    6264                $name = $parent->cat_name; 
    63  
     65                 
     66        // If this is not a top-level parent, first get the parent of this category. 
    6467        if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { 
    6568                $visited[] = $parent->parent; 
    66                 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited ); 
     69                $chain .= get_category_parents( $parent->parent, $link, $separator, $useslug, $visited, $trailing_separator, $use_desc_title ); 
     70                 
     71                // If the parent output did NOT contain a trailing separator 
     72                // we need to add the separator between category names/links 
     73                if( !$trailing_separator ) 
     74                        $chain .= $separator; 
     75                 
    6776        } 
    6877 
    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; 
     78        if ( $link ) { 
     79                $title = ""; 
     80                if ( $use_desc_title )  
     81                        $title = category_description( $parent->term_id ); 
     82                if ( $title == "") 
     83                        $title = esc_attr( sprintf( __( "View all posts in %s" ), $parent->cat_name ) ); 
     84                $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . $title . '">'.$name.'</a>'; 
     85        } 
     86        else { 
     87                // Add this category name onto the end of the string which already contains all of its parent names 
     88                $chain .= $name; 
     89        } 
     90                 
     91        if( $trailing_separator ) 
     92                $chain .= $separator; 
     93                         
    7394        return $chain; 
    7495} 
    7596