[wp-trac] [WordPress Trac] #7030: get_category_parents needs improvement

WordPress Trac wp-trac at lists.automattic.com
Fri May 23 15:25:33 GMT 2008


#7030: get_category_parents needs improvement
---------------------+------------------------------------------------------
 Reporter:  jayabb   |       Owner:  anonymous
     Type:  defect   |      Status:  new      
 Priority:  normal   |   Milestone:  2.5.2    
Component:  General  |     Version:  2.5.1    
 Severity:  normal   |    Keywords:           
---------------------+------------------------------------------------------
 get_category_parents is fine for internal use and generating URLs etc. but
 it also has a template tag version which could use some enhancements.

 Problems 1: It displays the separator at the end of the list as well as in
 between categories.

 Problem 2: There is no way to override the title attribute when generating
 links.

 Problem 3: This is more of a documentation and code readability issue than
 anything else, the "nicename" parameter is named back-to-front (i.e. FALSE
 means you get nice names and TRUE means you get slugs).

 The following replacement function fixes all three issues (issue 3 fixed
 in the parameter name, still needs fixing in the docs):


 {{{
 function get_category_parents($id, $link = FALSE, $separator = '/',
 $useslug = FALSE, $trailingseparator = TRUE, $usedescfortitle = FALSE)
 {
         $output = "";

         // Get the category from the id
         $category = &get_category($id);
         if(is_wp_error($category))
         {
                 return $category;
         }

         // Use the slug or the category name
         if($useslug)
         {
                 $name = $category->slug;
         }
         else
         {
                 $name = $category->cat_name;
         }

         // If this is not a top-level parent, first get the parent of this
 category.
         if($category->parent && ($category->parent != $category->term_id))
         {
                 $output = get_category_parents($category->parent, $link,
 $separator, $useslug, $trailingseparator, $usedescfortitle);

                 // If the parent output did NOT contain a trailing
 separator
                 // we need to add the separator between category
 names/links
                 if(!$trailingseparator)
                 {
                         $output .= $separator;
                 }
         }

         // Output a link or plain text
         if($link)
         {
                 $title = "";

                 // Use the category description or the default text for
 the link title
                 if($usedescfortitle)
                 {
                         $title = $category->description;
                 }

                 if($title == "")
                 {
                         $title = "View all posts in " . $name;
                 }

                 // Add this category link onto the end of the string which
 already contains all of its parent links
                 $output .= '<a href="' .
 get_category_link($category->term_id) . '" title="' . $title . '">' .
 $name . '</a>';
         }
         else
         {
                 // Add this category name onto the end of the string which
 already contains all of its parent names
                 $output .= $name;
         }

         // Add the trailing separator
         if($trailingseparator)
         {
                 $output .= $separator;
         }

         return $output;
 }
 }}}


 This updated function is backwards compatible with the old one, there
 should not be any behavioural differences. If someone could integrate and
 test it in the core for the next release it'd be much appreciated. Also
 change the docs so that nice name is called use slugs instead.

 Ta,
 -Jay

-- 
Ticket URL: <http://trac.wordpress.org/ticket/7030>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list