Opened 2 years ago
Last modified 20 months ago
#17069 reopened enhancement
missing get_term_parents() function
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Taxonomy | Version: | 3.1 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | rafaehlers |
Description
we got get_category_parents (http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/category-template.php#L30)
which use $parent = &get_category( $id ); limited to 'category' taxonomy, but there is nothing similar to other taxonomies. IMO we should rewrite this function to be generic and use it as get_term_parents function, the existing get_category_parents would use that get_term_parents, so something like (sorry, i do not know how to use diff) this:
42 function get_term_parents( $id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
43 $chain = '';
44 $parent = &get_term( $id, $taxonomy );
45 if ( is_wp_error( $parent ) )
46 return $parent;
47
48 if ( $nicename )
49 $name = $parent->slug;
50 else
51 $name = $parent->name;
52
53 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
54 $visited[] = $parent->parent;
55 $chain .= get_term_parents( $parent->parent, $link, $separator, $nicename, $visited );
56 }
57
58 if ( $link )
59 $chain .= '<a href="' . get_term_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
60 else
61 $chain .= $name.$separator;
62 return $chain;
63 }
and
function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
return get_term_parents ($id,'category',$separator,$nicename,$visited);
}
Attachments (2)
Change History (11)
- Milestone Awaiting Review deleted
- Resolution set to duplicate
- Status changed from new to closed
- Keywords needs-testing has-patch removed
- Milestone set to Future Release
- Resolution duplicate deleted
- Status changed from closed to reopened
I guess I was a bit hasty.
We could add a get_term_parents() template tag that uses get_ancestors() internally.
For creating patches: http://core.trac.wordpress.org/#HowtoSubmitPatches
comment:4
rafaehlers — 2 years ago
thomask: you missed on line 55 the argument $taxonomy
actual: $chain .= get_term_parents( $parent->parent, $link, $separator, $nicename, $visited );
should be: $chain .= get_term_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
comment:5
rafaehlers — 2 years ago
- Cc rafaehlers added
another argument missing on line 59 on function get_term_link, missed $taxonomy and use $parent->slug instead.
actual: $chain .= '<a href="' . get_term_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
should be: $chain .= '<a href="' . get_term_link( '''$parent->slug, $taxonomy''') . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
comment:6
SergeyBiryukov — 21 months ago
- Keywords has-patch added
- Keywords needs-patch added; has-patch removed
As I said, get_term_parents() should use get_ancestors(). And then, get_category_parents() should be made to use get_term_parents().
SergeyBiryukov — 21 months ago
comment:8
SergeyBiryukov — 21 months ago
- Keywords has-patch added; needs-patch removed
Right, updated the patch.
Looks like $visited parameter can be deprecated (it isn't documented in Codex anyway).
comment:9
SergeyBiryukov — 20 months ago
Related: #7030 (the latest patches overlap a lot)

We have something even better than get_term_parents(): get_ancestors().
See #12443