Advertisements

get_term_link

Definition:
function get_term_link( $term, $taxonomy = '') {}

Generates a permalink for a taxonomy term archive.

Parameters

  • object|int|string $term
  • string $taxonomy: (optional if $term is object)

Return values

returns:HTML link to taxonomy term archive on success, WP_Error if term does not exist.

Defined filters

  • tag_link
    apply_filters( 'tag_link', $termlink, $term->term_id )
  • category_link
    apply_filters( 'category_link', $termlink, $term->term_id )
  • term_link
    apply_filters('term_link', $termlink, $term, $taxonomy)

Source code

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

			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 )

		$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );

	elseif ( 'category' == $taxonomy )

		$termlink = apply_filters( 'category_link', $termlink, $term->term_id );



	return apply_filters('term_link', $termlink, $term, $taxonomy);

}

1761

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: