Opened 9 years ago
Closed 8 years ago
#35886 closed enhancement (duplicate)
get_term_parents function instead/inside get_category_parents
Reported by: | hosein71 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.4.2 |
Component: | Taxonomy | Keywords: | needs-patch needs-unit-tests |
Focuses: | Cc: |
Description
Simply add one more arguments in get_category_parents
function for getting custom taxonomy parents.
It can be like this:
function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
$chain = '';
$parent = get_term( $id, $taxonomy );
if ( is_wp_error( $parent ) )
return $parent;
if ( $nicename )
$name = $parent->slug;
else
$name = $parent->name;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
}
if ( $link )
$chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
else
$chain .= $name.$separator;
return $chain;
}
So developers can use it for getting any taxonomy parents.
The possible problem is: The given taxonomy is not Hierarchical.
The solution is checking taxonomy by is_taxonomy_hierarchical( $taxonomy )
and return false if it is not.
Attachments (2)
Change History (6)
#1
@
9 years ago
- Keywords needs-patch needs-unit-tests added
Hey there,
Thanks for creating this ticket and welcome to trac!
I think you mean terms, not taxonomies, though. You'd want to get the parents of a specific term. Basically, a taxonomy is a way to group things together. The names for the different groupings in a taxonomy are called terms. 'category' and 'tags' are the two default taxonomies in WordPress.
This means the function should be called something like get_term_parents()
. A $taxonomy
parameter is not needed, as a term ID is unique and we can get its taxonomy name from there. That's why the $taxonomy
parameter is optional for get_term()
as well.
Besides that, would you mind creating a proper patch with your suggested changes? See https://make.wordpress.org/core/handbook/tutorials/working-with-patches/ for more information.
#2
@
9 years ago
Hi,
You are true. It should be term instead of taxonomy and parameter is not needed. By the way get_term_parents()
can be a useful function. I will read documentation about patching and I will add a patch for it.
Thanks
Includes patched function.