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

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

  • 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. – Madelyn Cruz Nov 20 '15 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. – Madelyn Cruz Nov 21 '15 at 2:33
  • 1
    Ooppss. My mistake. I have used the wrong tid. Everything works fine. Thank you :) – Madelyn Cruz Nov 21 '15 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, privacy policy and cookie policy

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