Make WordPress Core

Changeset 30209


Ignore:
Timestamp:
11/03/2014 06:48:42 PM (10 years ago)
Author:
boonebgorges
Message:

Introduce term_template param to get_the_taxonomies().

This parameter allows theme and plugin authors to specify the formatting they
would like on the term links as they are parsed into the taxonomy list.

Props hereswhatidid, dlh, davidjlaietta.
See #27238.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r30205 r30209  
    41324132 * display the taxonomies for a specific post.
    41334133 *
    4134  * The available defaults are:
    4135  * 'post' : default is 0. The post ID to get taxonomies of.
    4136  * 'before' : default is empty string. Display before taxonomies list.
    4137  * 'sep' : default is empty string. Separate every taxonomy with value in this.
    4138  * 'after' : default is empty string. Display this after the taxonomies list.
    4139  * 'template' : The template to use for displaying the taxonomy terms.
    4140  *
    41414134 * @since 2.5.0
    41424135 *
    4143  * @param array $args Override the defaults.
     4136 * @param array $args {
     4137 *     Arguments about which post to use and how to format the output.
     4138 *
     4139 *     @type  int|WP_Post $post          Post ID or object to get taxonomies of. Default current post.
     4140 *     @type  string      $before        Displays before the taxonomies. Default empty string.
     4141 *     @type  string      $sep           Separates each taxonomy. Default is a space.
     4142 *     @type  string      $after         Displays after the taxonomies. Default empty string.
     4143 *     @type  string      $template      Template for displaying a taxonomy label and list of
     4144 *                                       terms. Default is "Label: Terms."
     4145 *     @type  string      $term_template Template for displaying a single term in the list.
     4146 *                                       Default is the term name linked to its archive.
     4147 * }
    41444148 */
    41454149function the_taxonomies( $args = array() ) {
     
    41494153        'sep' => ' ',
    41504154        'after' => '',
    4151         /* translators: %s: taxonomy label, %l: list of term links */
    4152         'template' => __( '%s: %l.' )
     4155        /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
     4156        'template' => __( '%s: %l.' ),
     4157        /* translators: %1$s: term link, %2$s: term name */
     4158        'term_template' => '<a href="%1$s">%2$s</a>',
    41534159    );
    41544160
     
    41744180
    41754181    $args = wp_parse_args( $args, array(
    4176         /* translators: %s: taxonomy label, %l: list of term links */
     4182        /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
    41774183        'template' => __( '%s: %l.' ),
     4184        /* translators: %1$s: term link, %2$s: term name */
     4185        'term_template' => '<a href="%1$s">%2$s</a>',
    41784186    ) );
    41794187
     
    41954203            $t['template'] = $args['template'];
    41964204        }
     4205        if ( empty( $t['term_template'] ) ) {
     4206            $t['term_template'] = $args['term_template'];
     4207        }
    41974208
    41984209        $terms = get_object_term_cache( $post->ID, $taxonomy );
     
    42034214
    42044215        foreach ( $terms as $term ) {
    4205             $links[] = "<a href='" . esc_attr( get_term_link( $term ) ) . "'>$term->name</a>";
     4216            $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name );
    42064217        }
    42074218        if ( $links ) {
  • trunk/tests/phpunit/tests/taxonomy.php

    r30141 r30209  
    5050    }
    5151
     52    /**
     53     * @group 27238
     54     */
     55    public function test_get_the_taxonomies_term_template() {
     56        $post_id = $this->factory->post->create();
     57
     58        $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '%2$s' ) );
     59        $this->assertEquals( 'Categories: Uncategorized.', $taxes['category'] );
     60
     61        $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '<span class="foo"><a href="%1$s">%2$s</a></span>' ) );
     62        $link = get_category_link( 1 );
     63        $this->assertEquals( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $taxes['category'] );
     64    }
     65
    5266    function test_the_taxonomies() {
    5367        $post_id = $this->factory->post->create();
     
    5872
    5973        $link = get_category_link( 1 );
    60         $expected = "Categories: <a href='$link'>Uncategorized</a>.";
     74        $expected = 'Categories: <a href="' . $link . '">Uncategorized</a>.';
    6175        $this->assertEquals( $expected, $output );
     76    }
     77
     78    /**
     79     * @group 27238
     80     */
     81    function test_the_taxonomies_term_template() {
     82        $post_id = $this->factory->post->create();
     83
     84        $output = get_echo( 'the_taxonomies', array( array( 'post' => $post_id, 'term_template' => '%2$s' ) ) );
     85        $this->assertEquals( 'Categories: Uncategorized.', $output );
     86
     87        $output = get_echo( 'the_taxonomies', array( array( 'post' => $post_id, 'term_template' => '<span class="foo"><a href="%1$s">%2$s</a></span>' ) ) );
     88        $link = get_category_link( 1 );
     89        $this->assertEquals( 'Categories: <span class="foo"><a href="' . $link . '">Uncategorized</a></span>.', $output );
    6290    }
    6391
Note: See TracChangeset for help on using the changeset viewer.