欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

得到term 的url

shiping1 的头像

Get Taxonomy path by tid

How do you get the path of a taxonomy term by it's tid. I am getting the taxonomy title and tid from the database, but I cannot figure out how to link it to its page. Any pointers?

Comments

If you have a term object you can use taxonomy_term_path().

I have tried that but it doesn't give me the right path. It only ever returns this, "taxonomy/term/".

Did you pass it a term object? (Like that return by taxonomy_get_term($tid))

yes I passed it a term id.

$termpath = taxonomy_term_path($term);

$term is the id selected from the database.

It worked when I did this, but I'm not getting the pathauto url.

$termobj = taxonomy_get_term($term);
$termpath = taxonomy_term_path($termobj);

Pass $termpath to l() if you want a link or url() if you want a path.

Thanks so much, that's exactly what I needed.

<?php
// first, get a term object from a term ID
$term = taxonomy_get_term($tid);
// then, use the term object to get the unaliased path
$taxonomy_path = taxonomy_term_path($term);
// finally, lookup the path alias using drupal_lookup_path()
$taxonomy_path_alias = drupal_lookup_path('alias', $taxonomy_path);
?>

Drupal architect. Dirt Biker.

If you feed $taxonomy_path to either l() or url() there is no need to look up the alias.

For Drupal 7 you can use: taxonomy_term_uri()

<?php
$term
= taxonomy_term_load($tid); // load term object
$term_uri = taxonomy_term_uri($term); // get array with path
$term_title = taxonomy_term_title($term);
$term_path = $term_uri['path'];
$link = l($term_title,$term_path);
?>

url('taxonomy/term/' . $tid);

来自 https://drupal.org/node/1016522

 

普通分类: