get_the_term_list()
最后更新于:2021-11-27 02:51:28
get_the_term_list( int$post_id, string$taxonomy, string$before=”, string$sep=”, string$after=”)Retrieves a post’s terms as a list with specified format.
参数
- $post_id
-
(int) (Required) Post ID.
- $taxonomy
-
(string) (Required) Taxonomy name.
- $before
-
(string) (Optional) String to use before the terms.
Default value: ”
- $sep
-
(string) (Optional) String to use between the terms.
Default value: ”
- $after
-
(string) (Optional) String to use after the terms.
Default value: ”
响应
(string|false|GC_Error) A list of terms on success, false if there are no terms, GC_Error on failure.
源文件
文件: gc-includes/category-template.php
function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_gc_error( $terms ) ) {
return $terms;
}
if ( empty( $terms ) ) {
return false;
}
$links = array();
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_gc_error( $link ) ) {
return $link;
}
$links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
/**
* Filters the term links for a given taxonomy.
*
* The dynamic portion of the filter name, `$taxonomy`, refers
* to the taxonomy slug.
*
* @since 2.5.0
*
* @param string[] $links An array of term links.
*/
$term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore GeChiUI.NamingConventions.ValidHookName.UseUnderscores
return $before . implode( $sep, $term_links ) . $after;
}