2

im following this great podcast which is drupal 5 (i guess).

I want to add a View into my page--taxonomy.tpl.php and get the id of the taxonomy term im currently looking at. Could maybe anybody translate this code snippet for drupal 7?

<?php
$view = views_get_view('taxonomypages');
$display = $view->execute_display('block_1', 2);
print $display['content'];
?>

I figured out that this shows me the view:

$view = views_get_view('taxonomypages');
print $view->preview('block_1',$tids);

but $tids is undefined. i need to have the current taxonomy term id that im looking at e.g when im at taxonomy/term/12 the id is 12 but $tids is undefined.

  • 1
    Are you trying to show a custom view instead of the standard taxonomy term page? If so, just enable the "Taxonomy term" view that is disabled by default and modify it to your liking. – uwe Jan 13 '12 at 2:59
  • 1
    What MotoTribe said sounds like the "Drupal Way". You can then use arguments from the url to get the id of the taxonomy term – WestieUK Jan 13 '12 at 8:08
1

I never used "page--taxonomy.tpl.php", but based upon what you want to do, I guess your looking for taxonomy-term.tpl.php.

In Drupal 7, you have a $term variable, from which you can get the tid. Below is some D7 code I've used for a similar purpose, that I adapted a bit to yours. Note that the argument to Views must be in an array.

$view = views_get_view('taxonomypages');
$view->execute_display('panel_pane_1', array($term->tid));
$output = $view->render();

But I do not recommend you proceed down this path. Instead use Panels, and pass the argument to a View that way. Yes, it will require a bit more configuration, but it will also be much more sustainable, easier to maintain, and keep less logic in the template files.

Relevant panels tutorials here.

| improve this answer | |
  • thanks for your answer! you missed a $ in the last line. But anyway im getting an error message that $term is undefined "Notice: Undefined variable: term in include() (..." :( – bench Jan 21 '12 at 16:02
  • 1
    Thanks, I added the missing $. Which file exactly are you in? I added a note about the D7 file I think you should be using. Again, I would go with Panels instead. – Letharion Jan 21 '12 at 16:45
0

When you already are on the term page it is possible to use the URL argument to determine the term id. The term id should be in arg(2).

$tid = arg(2);
| improve this answer | |

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.