Im on d7. Im trying to get the taxonomy term name of the the taxonomy page that I am on. Since Im not on a "node" page, I can't get it by the $node->tid. What should I do? Thanks.
3 Answers
Try..
http://api.drupal.org/api/drupal/modules--taxonomy--taxonomy.module/function/taxonomy_term_load/7
$term = taxonomy_term_load(arg(2)); $title = $term->name; arg(2) should return the tid of the taxonomy page (taxonomy/term/tid).
- Thanks for the quick reply. I tried your method, but I'm getting this error
Notice: Trying to get property of non-object in include() (line 79 of {my site}/templates/page.tpl.php)and its not working. :(oobie11– oobie112012-01-27 02:15:30 +00:00Commented Jan 27, 2012 at 2:15 - What does
print_r(arg())show?2012-01-27 03:55:13 +00:00Commented Jan 27, 2012 at 3:55 - Array ( [0] => taxonomy [1] => term [2] => 1920 )oobie11– oobie112012-01-27 05:49:56 +00:00Commented Jan 27, 2012 at 5:49
- figured it out, Ill explain belowoobie11– oobie112012-01-27 05:56:29 +00:00Commented Jan 27, 2012 at 5:56
I figured it out, heres what I did:
<?php $termid = arg(2); $term = taxonomy_term_load($termid); $title = $term->name; ?> Thank you @Kevin for your help.
- +1 for saving taxonomy-term-id is separate variable. It follows KISS principle(atleast from my point-of-view).Bhavik Shah– Bhavik Shah2013-12-30 12:50:30 +00:00Commented Dec 30, 2013 at 12:50
- Works perfectly, took me a little while to find a correct answer so thank you.JDavies– JDavies2016-07-28 09:11:40 +00:00Commented Jul 28, 2016 at 9:11
Instead of using taxonomy_term_load() which is not working well every time, you can call:
function get_tag_name($tid) { $query = db_select('taxonomy_term_data', 't'); $query ->condition('t.tid', $tid, '=') ->fields('t', array('tid', 'name')); $result = $query->execute(); foreach ($result as $row) { return $row->name; } }