Changeset 32553
- Timestamp:
- 05/23/2015 06:28:22 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r32498 r32553 4382 4382 * 4383 4383 * @since 2.5.0 4384 * @since 4.3.0 Introduced `$field` argument. 4384 4385 * 4385 4386 * @param object|int|string $term The term object, ID, or slug whose link will be retrieved. 4386 4387 * @param string $taxonomy Optional. Taxonomy. Default empty. 4388 * @param string $field Optional. The term field that should be matched by the `$term` argument. Accepts 4389 * any `$field` values accepted by `get_term_by()`: 'slug', 'name', 4390 * 'term_taxonomy_id', or 'id'. Default is 'slug', unless `$term` is an integer, in 4391 * which case it's asssumed to be an ID. 4387 4392 * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist. 4388 4393 */ 4389 function get_term_link( $term, $taxonomy = '' ) {4394 function get_term_link( $term, $taxonomy = '', $field = null ) { 4390 4395 global $wp_rewrite; 4391 4396 4392 4397 if ( !is_object($term) ) { 4393 if ( is_ int($term) ) {4394 $ term = get_term($term, $taxonomy);4395 } else {4396 $term = get_term_by('slug', $term, $taxonomy); 4397 }4398 if ( is_null( $field ) ) { 4399 $field = is_int( $term ) ? 'id' : 'slug'; 4400 } 4401 4402 $term = get_term_by( $field, $term, $taxonomy ); 4398 4403 } 4399 4404 -
trunk/tests/phpunit/tests/term/getTermLink.php
r32552 r32553 55 55 $term = (string) $t1; 56 56 57 $actual = get_term_link( $term, 'wptests_tax', 'id' ); 58 $this->assertContains( 'wptests_tax=foo', $actual ); 59 } 60 61 /** 62 * @ticket 14156 63 */ 64 public function test_should_match_field_by_name() { 65 $t = $this->factory->term->create( array( 66 'taxonomy' => 'wptests_tax', 67 'slug' => 'foo', 68 'name' => 'Bar Term', 69 ) ); 70 71 $actual = get_term_link( 'Bar Term', 'wptests_tax', 'name' ); 72 $this->assertContains( 'wptests_tax=foo', $actual ); 73 } 74 75 /** 76 * @ticket 14156 77 */ 78 public function test_should_match_field_by_tt_id() { 79 $t = $this->factory->term->create( array( 80 'taxonomy' => 'wptests_tax', 81 'slug' => 'foo', 82 'name' => 'Bar Term', 83 ) ); 84 85 $actual = get_term_link( 'Bar Term', 'wptests_tax', 'name' ); 86 $this->assertContains( 'wptests_tax=foo', $actual ); 87 } 88 89 /** 90 * @ticket 14156 91 */ 92 public function test_numeric_string_should_be_interpreted_as_term_id_if_id_field_is_specified() { 93 $t1 = $this->factory->term->create( array( 94 'taxonomy' => 'wptests_tax', 95 'name' => 'foo', 96 ) ); 97 $t2 = $this->factory->term->create( array( 98 'taxonomy' => 'wptests_tax', 99 'slug' => $t1, 100 ) ); 101 102 $term = (string) $t1; 103 57 104 $actual = get_term_link( $term, 'wptests_tax' ); 58 105 $this->assertContains( 'wptests_tax=' . $term, $actual );
Note: See TracChangeset
for help on using the changeset viewer.