WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 3 months ago

#17069 reopened enhancement

missing get_term_parents() function

Reported by: thomask Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords: has-patch needs-refresh needs-unit-tests
Focuses: Cc:

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)

category-template.patch (1.6 KB) - added by rafaehlers 4 years ago.
the patch with the new function
17069.patch (2.7 KB) - added by SergeyBiryukov 4 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 @scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

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

See #12443

comment:2 @scribu4 years ago

Granted, it returns a raw array, not a list of links.

comment:3 @scribu4 years ago

  • 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 @rafaehlers4 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 @rafaehlers4 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;

Last edited 3 years ago by scribu (previous) (diff)

@rafaehlers4 years ago

the patch with the new function

comment:6 @SergeyBiryukov4 years ago

  • Keywords has-patch added

comment:7 @scribu4 years ago

  • 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().

@SergeyBiryukov4 years ago

comment:8 @SergeyBiryukov4 years 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 @SergeyBiryukov4 years ago

Related: #7030 (the latest patches overlap a lot)

comment:10 @alexvorn22 years ago

so closing? I would like this patch to be included in the core.

comment:11 @wonderboymusic2 years ago

  • Keywords needs-refresh added

comment:12 @boonebgorges3 months ago

  • Keywords needs-unit-tests added
Note: See TracTickets for help on using tickets.