1

This code below displays the current taxonomy term on a taxonomy term page

$current = taxonomy_term_load(arg(2)); ?>
if ($current): ?>
echo $current->name; ?>
endif; ?>

The code below displays the error: "Notice: Trying to get property of non-object in eval() (line 9 of...) Line 9 is "echo $parent->name;"

$parent = taxonomy_get_parents($current->tid);
if ($parent){
echo $parent->name;
}?>

I'm really confused as to why the first set of code works but the 2nd doesn't? "print_r($parent);" displays the info below, so the data is there.

Array ( [3218] => stdClass Object ( [tid] => 3218 [vid] => 12 [name] => NAME OF TAXONOMY TERM [description] => This is the description area [format] => full_html [weight] => 0 [vocabulary_machine_name] => mfr_model [field_displayed_view] => Array ( [und] => Array ( [0] => Array ( [vname] => final_term_pages|testing [vargs] => ) ) ) [metatags] => Array ( [und] => Array ( [title] => Array ( [value] => This is the meta page title ) [description] => Array ( [value] => This is the meta description ) ) ) ) )

4
  • I should say that both these groups of code are in the same newly formed block using the php filter. Jan 19, 2015 at 6:40
  • Because taxonomy_get_parents returns An array of term objects...?
    – Clive
    Jan 19, 2015 at 11:06
  • I know very little about php, but I see that now. If I print $current, it shows as a "stdClass Object" and not an array as the second group of code. My question would be then, how do I print the [name} part of the array only? Jan 19, 2015 at 15:18
  • There are quite a few ways - $x = array_shift($parent); echo $x->name; would be one. echo reset($parent)->name; would be another, etc
    – Clive
    Jan 19, 2015 at 16:37

1 Answer 1

0

If you check out the docs for taxonomy_get_parents() you can see that the return value is an array of parent terms.

So you might loop through the results like this:

$parents = taxonomy_get_parents($current->tid);
foreach ($parents as $parent) {
  echo $parent->name;
}

or use just the first one like this:

$parents = taxonomy_get_parents($current->tid);
if ($parents) {
  echo $parents[0]->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.