返回介绍

get_term_link()

发布于 2017-09-11 00:22:20 字数 7333 浏览 943 评论 0 收藏 0

get_term_link( object|int|string $term,  string $taxonomy = '' )

Generate a permalink for a taxonomy term archive.


description


参数

$term

(object|int|string) (Required) The term object, ID, or slug whose link will be retrieved.

$taxonomy

(string) (Optional) Taxonomy.

Default value: ''


返回值

(string|WP_Error) HTML link to taxonomy term archive on success, WP_Error if term does not exist.


源代码

File: wp-includes/taxonomy.php

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

	$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 ( $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
		 * @deprecated 2.5.0 Use 'term_link' instead.
		 *
		 * @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
		 * @deprecated 2.5.0 Use 'term_link' instead.
		 *
		 * @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 object $term     Term object.
	 * @param string $taxonomy Taxonomy slug.
	 */
	return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}

更新日志

Versiondescription
2.5.0Introduced.

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.


相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/taxonomy.php: get_ancestors()
  • wp-includes/taxonomy.php: tag_link
  • wp-includes/taxonomy.php: category_link
  • wp-includes/taxonomy.php: term_link
  • wp-includes/taxonomy.php: get_term_by()
  • wp-includes/taxonomy.php: get_term()
  • wp-includes/taxonomy.php: get_taxonomy()
  • wp-includes/link-template.php: home_url()
  • wp-includes/link-template.php: user_trailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::get_extra_permastruct()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 9 more uses Hide more uses

Used By

  • wp-includes/category-template.php: get_term_parents_list()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php: WP_REST_Terms_Controller::prepare_item_for_response()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::search_available_items_query()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::load_available_items_query()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::handle_row_actions()
  • wp-includes/category-template.php: get_the_term_list()
  • wp-includes/class-walker-category.php: Walker_Category::start_el()
  • wp-includes/category-template.php: wp_tag_cloud()
  • wp-includes/category-template.php: get_tag_link()
  • wp-includes/category-template.php: get_category_link()
  • wp-includes/taxonomy.php: get_the_taxonomies()
  • wp-includes/link-template.php: get_term_feed_link()
  • wp-includes/admin-bar.php: wp_admin_bar_edit_menu()
  • wp-includes/canonical.php: redirect_canonical()
  • wp-includes/post-formats.php: get_post_format_link()
  • wp-includes/nav-menu.php: wp_setup_nav_menu_item()
  • Show 12 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Codex
    
    $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_wp_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>';
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文