get_term_link()
最后更新于:2021-11-27 00:57:09
get_term_link( GC_Term|int|string$term, string$taxonomy=”)Generate a permalink for a taxonomy term archive.
参数
- $term
-
(GC_Term|int|string) (Required) The term object, ID, or slug whose link will be retrieved.
- $taxonomy
-
(string) (Optional) Taxonomy.
Default value: ”
响应
(string|GC_Error) URL of the taxonomy term archive on success, GC_Error if term does not exist.
源文件
文件: gc-includes/taxonomy.php
function get_term_link( $term, $taxonomy = '' ) {
global $gc_rewrite;
if ( ! is_object( $term ) ) {
if ( is_int( $term ) ) {
$term = get_term( $term, $taxonomy );
} else {
$term = get_term_by( 'slug', $term, $taxonomy );
}
}
if ( ! is_object( $term ) ) {
$term = new GC_Error( 'invalid_term', __( 'Empty Term.' ) );
}
if ( is_gc_error( $term ) ) {
return $term;
}
$taxonomy = $term->taxonomy;
$termlink = $gc_rewrite->get_extra_permastruct( $taxonomy );
/**
* Filters the permalink structure for a term before token replacement occurs.
*
* @since 4.9.0
*
* @param string $termlink The permalink structure for the term's taxonomy.
* @param GC_Term $term The term object.
*/
$termlink = apply_filters( 'pre_term_link', $termlink, $term );
$slug = $term->slug;
$t = get_taxonomy( $taxonomy );
if ( empty( $termlink ) ) {
if ( 'category' === $taxonomy ) {
$termlink = '?cat=' . $term->term_id;
} elseif ( $t->query_var ) {
$termlink = "?$t->query_var=$slug";
} else {
$termlink = "?taxonomy=$taxonomy&term=$slug";
}
$termlink = home_url( $termlink );
} else {
if ( ! empty( $t->rewrite['hierarchical'] ) ) {
$hierarchical_slugs = array();
$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
foreach ( (array) $ancestors as $ancestor ) {
$ancestor_term = get_term( $ancestor, $taxonomy );
$hierarchical_slugs[] = $ancestor_term->slug;
}
$hierarchical_slugs = array_reverse( $hierarchical_slugs );
$hierarchical_slugs[] = $slug;
$termlink = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink );
} else {
$termlink = str_replace( "%$taxonomy%", $slug, $termlink );
}
$termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
}
// Back compat filters.
if ( 'post_tag' === $taxonomy ) {
/**
* Filters the tag link.
*
* @since 2.3.0
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter.
* @since 5.4.1 Restored (un-deprecated).
*
* @param string $termlink Tag link URL.
* @param int $term_id Term ID.
*/
$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
} elseif ( 'category' === $taxonomy ) {
/**
* Filters the category link.
*
* @since 1.5.0
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter.
* @since 5.4.1 Restored (un-deprecated).
*
* @param string $termlink Category link URL.
* @param int $term_id Term ID.
*/
$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
}
/**
* Filters the term link.
*
* @since 2.5.0
*
* @param string $termlink Term link URL.
* @param GC_Term $term Term object.
* @param string $taxonomy Taxonomy slug.
*/
return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}
$terms = get_terms( 'species' ); echo '<ul>'; foreach ( $terms as $term ) { // The $term is an object, so we don't need to specify the $taxonomy. $term_link = get_term_link( $term ); // If there was an error, continue to the next term. if ( is_gc_error( $term_link ) ) { continue; } // We successfully got a link. Print it out. echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>'; } echo '</ul>';