Make WordPress Core

Opened 16 years ago

Closed 8 years ago

#7030 closed enhancement (wontfix)

get_category_parents needs improvement

Reported by: jayabb's profile jayabb Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.5.1
Component: Taxonomy Keywords: has-patch needs-testing needs-refresh close
Focuses: template Cc:


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);
		return $category;

	// Use the slug or the category name
		$name = $category->slug;
		$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
			$output .= $separator;

	// Output a link or plain text
		$title = "";

		// Use the category description or the default text for the link title
			$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>';
		// Add this category name onto the end of the string which already contains all of its parent names
		$output .= $name;

	// Add the trailing separator
		$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.


Attachments (2)

7030.diff (2.8 KB) - added by wojtek.szkutnik 14 years ago.
7030.get_ancestors.diff (2.2 KB) - added by scribu 13 years ago.
Use get_ancestors() in get_category_parents

Download all attachments as: .zip

Change History (12)

#1 @ryan
16 years ago

  • Milestone changed from 2.5.2 to 2.9

Milestone 2.5.2 deleted

#2 @Denis-de-Bernardy
15 years ago

  • Component changed from General to Template

#3 @ryan
14 years ago

  • Milestone changed from 2.9 to Future Release
  • Type changed from defect (bug) to enhancement

#4 @wojtek.szkutnik
14 years ago

  • Cc wojtek.szkutnik@… added
  • Keywords has-patch needs-testing gsoc added

13 years ago

Use get_ancestors() in get_category_parents

#5 @scribu
13 years ago

In 7030.get_ancestors.diff the $visited argument is deprecated, since it's not needed anymore.

We should deprecate the entire function, since the parameters are a mess.

#7 @nacin
10 years ago

  • Component changed from Template to Taxonomy
  • Focuses template added

#8 @chriscct7
8 years ago

  • Keywords needs-refresh added; gsoc removed

#9 follow-up: @rachelbaker
8 years ago

  • Keywords close added

With little forward movement or attention here in the past 5 years, I suggest closing this ticket. If you feel like there is a need to make these changes to get_category_parents() targeted toward the template tag use-case, please chime in.

#10 in reply to: ↑ 9 @boonebgorges
8 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.