11

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 3

23

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

4
  • 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
    Jan 27, 2012 at 2:15
  • What does print_r(arg()) show?
    – mpdonadio
    Jan 27, 2012 at 3:55
  • Array ( [0] => taxonomy [1] => term [2] => 1920 )
    – oobie11
    Jan 27, 2012 at 5:49
  • figured it out, Ill explain below
    – oobie11
    Jan 27, 2012 at 5:56
6

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.

2
  • +1 for saving taxonomy-term-id is separate variable. It follows KISS principle(atleast from my point-of-view). Dec 30, 2013 at 12:50
  • Works perfectly, took me a little while to find a correct answer so thank you.
    – JDavies
    Jul 28, 2016 at 9:11
-4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.