1

Using Drupal 7, I want to create a simple block that will be on all taxonomy term pages that will show the first parent of the current term (there will be a tree of parents, but I want just the next highest).

The below code works to pull the current term name, but I'm unsure how to go about pulling the parent term of this term page.

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

Any suggestions?

2 Answers 2

2

I had to create a new question to get the answer (PHP is not my strong point).

$parent is actually an array, so it's output is a little different. There's likely many ways of doing it, but

echo reset($parent)->name;

worked for me.

1
  • if $parent is an array, the correct way to access it is $parent['name']
    – Beebee
    Jan 20, 2015 at 0:26
1

You get the parent term with taxonomy_get_parents($tid). So you can just add:

$parent = taxonomy_get_parents($current->tid);
if ($parent){
  echo $parent->name;
}
4
  • Thanks for the help, but I am getting an error with the first line. Notice: Undefined variable, and Notice: Trying to get property of non-object in eval() Jan 7, 2015 at 17:05
  • Silly me, I removed the previous code so there was no $current variable. I added it back, now I only have the error "Notice: Trying to get property of non-object in eval() " on the code line "echo $parent->name;". Jan 8, 2015 at 1:28
  • This is also being used in a simple text block with the PHP filter option, not sure if that is causing this not to work as maybe this code is to be used in a template only? Jan 8, 2015 at 1:31
  • Adding print_r($parent); shows that it is pulling the data correctly, I just don't know PHP/Drupal well enough to know why echo $parent->name; is not displaying properly. Jan 8, 2015 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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