Make WordPress Core

Ticket #42363: get_the_term_list.php

File get_the_term_list.php, 1.5 KB (added by francescotaurino, 6 years ago)

get_the_term_list function

Line 
1/**
2 * Retrieve a post's terms as a list with specified format.
3 *
4 * @since 2.5.0
5 *
6 * @param int $id Post ID.
7 * @param string $taxonomy Taxonomy name.
8 * @param string $before Optional. Before list.
9 * @param string $sep Optional. Separate items using this.
10 * @param string $after Optional. After list.
11 * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
12 */
13function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
14        $terms = get_the_terms( $id, $taxonomy );
15
16        if ( is_wp_error( $terms ) )
17                return $terms;
18
19        if ( empty( $terms ) )
20                return false;
21
22        $links = array();
23
24        foreach ( $terms as $term ) {
25                $link = get_term_link( $term, $taxonomy );
26                if ( is_wp_error( $link ) ) {
27                        return $link;
28                }
29               
30                /**
31                 * Filters the term link for a given taxonomy.
32                 *
33                 * The dynamic portion of the filter name, `$taxonomy`, refers
34                 * to the taxonomy slug.
35                 *
36                 * @since 4.8.4
37                 *
38                 * @param string HTML link.
39                 * @param object $term WP_Term object
40                 */
41                $links[] = apply_filters( "term_link-{$taxonomy}", '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>', $term );
42        }
43
44        /**
45         * Filters the term links for a given taxonomy.
46         *
47         * The dynamic portion of the filter name, `$taxonomy`, refers
48         * to the taxonomy slug.
49         *
50         * @since 2.5.0
51         *
52         * @param array $links An array of term links.
53         */
54        $term_links = apply_filters( "term_links-{$taxonomy}", $links );
55
56        return $before . join( $sep, $term_links ) . $after;
57}