On this page
get_term_link( WP_Term|int|string $term, string $taxonomy = '' ): string|WP_Error
Generates a permalink for a taxonomy term archive.
Parameters
$termWP_Term|int|string Required-
The term object, ID, or slug whose link will be retrieved.
$taxonomystring Optional-
Taxonomy.
Default:
''
Return
string|WP_Error URL of the taxonomy term archive on success, WP_Error if term does not exist.
More Information
- Since the term can be an object, integer, or string, make sure that any numbers you pass in are explicitly converted to an integer (example: (int) $term_id). Otherwise the function will assume that $term is a slug instead of a term ID.
- PHP may halt if you attempt to print an error result ("Catchable fatal error: Object of class WP_Error could not be converted to string"). You should always use is_wp_error() to check the result of this function, in case the term does not exist.
Source
File: wp-includes/taxonomy.php. View all references
function get_term_link( $term, $taxonomy = '' ) {
global $wp_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 WP_Error( 'invalid_term', __( 'Empty Term.' ) );
}
if ( is_wp_error( $term ) ) {
return $term;
}
$taxonomy = $term->taxonomy;
$termlink = $wp_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 WP_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 WP_Term $term Term object.
* @param string $taxonomy Taxonomy slug.
*/
return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}
Hooks
- apply_filters( 'category_link',
string $termlink ,int $term_id ) -
Filters the category link.
- apply_filters( 'pre_term_link',
string $termlink ,WP_Term $term ) -
Filters the permalink structure for a term before token replacement occurs.
- apply_filters( 'tag_link',
string $termlink ,int $term_id ) -
Filters the tag link.
- apply_filters( 'term_link',
string $termlink ,WP_Term $term ,string $taxonomy ) -
Filters the term link.
Related
Uses
| Uses | Description |
|---|---|
| get_ancestors() wp-includes/taxonomy.php | Gets an array of ancestor IDs for a given object. |
| get_term_by() wp-includes/taxonomy.php | Gets all term data from database by term field and data. |
| user_trailingslashit() wp-includes/link-template.php | Retrieves a trailing-slashed string if the site is set for adding trailing slashes. |
| WP_Rewrite::get_extra_permastruct() wp-includes/class-wp-rewrite.php | Retrieves an extra permalink structure by name. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| get_term() wp-includes/taxonomy.php | Gets all term data from database by term ID. |
| get_taxonomy() wp-includes/taxonomy.php | Retrieves the taxonomy object of $taxonomy. |
| home_url() wp-includes/link-template.php | Retrieves the URL for the current site where the front end is accessible. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Term_Search_Handler::prepare_item() wp-includes/rest-api/search/class-wp-rest-term-search-handler.php | Prepares the search result for a given ID. |
| WP_Sitemaps_Taxonomies::get_url_list() wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php | Gets a URL list for a taxonomy sitemap. |
| get_term_parents_list() wp-includes/category-template.php | Retrieves term parents with separator. |
| WP_REST_Terms_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php | Prepares a single term output for response. |
| WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() wp-includes/customize/class-wp-customize-nav-menu-item-setting.php | Get the value emulated into a WP_Post and set up as a nav_menu_item. |
| WP_Customize_Nav_Menus::search_available_items_query() wp-includes/class-wp-customize-nav-menus.php | Performs post queries for available-item searching. |
| WP_Customize_Nav_Menus::load_available_items_query() wp-includes/class-wp-customize-nav-menus.php | Performs the post_type and taxonomy queries for loading available menu items. |
| WP_Terms_List_Table::handle_row_actions() wp-admin/includes/class-wp-terms-list-table.php | Generates and displays row action links. |
| Walker_Category::start_el() wp-includes/class-walker-category.php | Starts the element output. |
| get_the_term_list() wp-includes/category-template.php | Retrieves a post’s terms as a list with specified format. |
| wp_tag_cloud() wp-includes/category-template.php | Displays a tag cloud. |
| get_category_link() wp-includes/category-template.php | Retrieves category link URL. |
| get_the_taxonomies() wp-includes/taxonomy.php | Retrieves all taxonomies associated with a post. |
| get_term_feed_link() wp-includes/link-template.php | Retrieves the feed link for a term. |
| wp_admin_bar_edit_menu() wp-includes/admin-bar.php | Provides an edit link for posts and terms. |
| redirect_canonical() wp-includes/canonical.php | Redirects incoming links to the proper URL based on the site url. |
| get_post_format_link() wp-includes/post-formats.php | Returns a link to a post format index. |
| wp_setup_nav_menu_item() wp-includes/nav-menu.php | Decorates a menu item object with the shared navigation menu item properties. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_term_link