2

Is there any solution on how theme the taxonomy term page per depth? Though this is doable with Views + TVI, we don't usually allow admin to manage TVI, but they're able to add new term and they're allowed to rearrange the terms. I would like that each depth has a different layout, but automatically changing based from its depth. This can done through hooks or how to achieve this in views?

For instance:

if ($depth == 0) {
  print $views_0;
}
elseif ($depth == 1) {
  print $views_1;
}
elseif ($depth == 2) {
  print $views_2;
}
else {
  print $views_default;
}

and so on...

2 Answers 2

1

Create a funtion to retrive term depth

//Gets the depth of the term id
function MYMODULE_term_depth($tid) {
  $parent = db_query("SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = :tid", array(':tid' => $tid))->fetchField();
  if($parent == 0) {
    return 1;
  }else  {
    return 1+MYMODULE_term_depth($parent);
  }
}

And Print the views based on depth

$depth = MYMODULE_term_depth($tid)
if ($depth == 0) {
  print views_embed_view('views_0');
}
elseif ($depth == 1) {
  print views_embed_view('views_1');
}
elseif ($depth == 2) {
  print views_embed_view('views_2');
}
else {
  print views_embed_view('views_default');
}

pass the view machine name to views_embed_view

3
  • Thank you for your quick response. Looks like this is what I need. I'll give it a try and will comment later. Thank you.
    – Maddie
    Commented Nov 20, 2015 at 0:08
  • Hi cmsbots.com, I've made a test and check each terms $depth, I'm just confused why the parent term value is 2 and the second level value is 1, while the third level and the following level gives correct value.
    – Maddie
    Commented Nov 21, 2015 at 2:33
  • 1
    Ooppss. My mistake. I have used the wrong tid. Everything works fine. Thank you :)
    – Maddie
    Commented Nov 21, 2015 at 3:28
0

The easiest way is probably to use taxonomy_get_parents_all():

$depth = count(taxonomy_get_parents_all($tid));

Your Answer

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

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