WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#42363 closed enhancement (maybelater)

Add a filter into get_the_term_list function.

Reported by: francescotaurino Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8.2
Component: Taxonomy Keywords:
Focuses: template Cc:
PR Number:

Description

This allows you to create a custom link and use the WP_Term object.

<?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 );
        }

Attachments (1)

get_the_term_list.php (1.5 KB) - added by francescotaurino 2 years ago.
get_the_term_list function

Download all attachments as: .zip

Change History (3)

@francescotaurino
2 years ago

get_the_term_list function

#1 @francescotaurino
2 years ago

  • Resolution set to maybelater
  • Status changed from new to closed

#2 @netweb
2 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.