[wp-trac] [WordPress Trac] #42363: Add a filter into get_the_term_list function.

WordPress Trac noreply at wordpress.org
Fri Oct 27 22:07:32 UTC 2017


#42363: Add a filter into get_the_term_list function.
------------------------------+-----------------------------
 Reporter:  francescotaurino  |      Owner:
     Type:  enhancement       |     Status:  new
 Priority:  normal            |  Milestone:  Awaiting Review
Component:  Taxonomy          |    Version:  4.8.2
 Severity:  normal            |   Keywords:
  Focuses:  template          |
------------------------------+-----------------------------
 This allows you to create a custom link and use the WP_Term object.


 {{{#!php
 <?php

 /**
  * Retrieve a post's terms as a list with specified format.
  *
  * @since 2.5.0
  *
  * @param int $id Post ID.
  * @param string $taxonomy Taxonomy name.
  * @param string $before Optional. Before list.
  * @param string $sep Optional. Separate items using this.
  * @param string $after Optional. After list.
  * @return string|false|WP_Error A list of terms on success, false if
 there are no terms, WP_Error on failure.
  */
 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '',
 $after = '' ) {
         $terms = get_the_terms( $id, $taxonomy );

         if ( is_wp_error( $terms ) )
                 return $terms;

         if ( empty( $terms ) )
                 return false;

         $links = array();

         foreach ( $terms as $term ) {
                 $link = get_term_link( $term, $taxonomy );
                 if ( is_wp_error( $link ) ) {
                         return $link;
                 }

                 /**
                  * Filters the term link for a given taxonomy.
                  *
                  * The dynamic portion of the filter name, `$taxonomy`,
 refers
                  * to the taxonomy slug.
                  *
                  * @since 4.8.4
                  *
                  * @param string HTML link.
                  * @param object $term WP_Term object
                  */
                 $links[] = apply_filters( "term_link-{$taxonomy}", '<a
 href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>', $term
 );
         }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/42363>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list