Drupal Theme from Scratch Drupal Theme from Scratch icon
Theming a Drupal 7 Website



The Drupal 7 Taxonomy Page Template

Another thing I learned by using comments (as mentioned before under "Drupal Template Tip!"), is if you have taxonomy term links pointing to taxonomy pages, Drupal will use the page.tpl.php template and look for a taxonomy template. At the same time, and here's where it get's funky, Drupal also uses your node template for each node entry on your taxonomy page.

As you can see in the diagram, I've created a taxonomy-term--event-tags.tpl.php template which allows me to add content above the node instances (so if I want to add a header and some text to describe my page, I would add it to this template). This template does not control the node instances themselves. Drupal uses my node--event.tpl.php template to wrap each node's content. This became a problem at first because it was showing ALL the content from each node (that makes for a long page).

Once I realized Drupal was using a node level template with my taxonomy template, I added a PHP IF statement to my node--event.tpl.php template to show different content for two different scenarios.

But before I can do that, I needed to identify which scenario is the current scenario. So I added the following code to the top of the node--event.tpl.php template...

<?php
// get current URL info to determine which layout to render...
$currentURL = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$currentURLarray = explode('/', $currentURL);
$urlArrayCountMinusThree = count($currentURLarray) -3;
$urlArrayCountMinusTwo = count($currentURLarray) -2;
$urlArrayCountMinusOne = count($currentURLarray) -1;
$lastURLsegment = $currentURLarray[$urlArrayCountMinusOne];
$secondToLastURLsegment = $currentURLarray[$urlArrayCountMinusTwo];
$thirdToLastURLsegment = $currentURLarray[$urlArrayCountMinusThree];
?>

This PHP code grabs the current URL, breaks it up into an array by using the "/" as separators and looks at the last and second from the last item in the array.

URL: https://www.examplesite.com/event/annual-good-time-picnic
Array: 
  0 - https:
  1 - 
  2 - www.examplesite.com
  3 - event
  4 - annual-good-time-picnic

Then, based on the results, the IF statement controls what to render...

<?php
if($secondToLastURLsegment == 'event' || 
  $thirdToLastURLsegment == 'event' || 
  $secondToLastURLsegment == "node" || 
  $thirdToLastURLsegment == "node"){
?>
  ...render ALL of the Event's content...
<?php } else { ?>
  ...render LESS of the Event's content...
<?php } ?>

This PHP code is checking to see if the second from the last item or the third from the last item is equal to 'event'. It has to check the third from last item in case someone somewhere adds another "/" to the end of the URL.

It also checks to see if either of those items is equal to the word 'node'. Since Drupal's default URLs look like this: https://www.examplesite.com/node/1071, it is possible that the current URL passed to this code might as well.

If any of these scenarios is true, then the template knows that this is an actual Event page and ALL of the content for that page will be displayed (or whatever content you decide to show).

If none of these scenarios is true, then the template will know that this is a taxonomy page and will only display what you have chosen to display for the taxonomy page items.

Drupal already knows that you are trying to pull up Event related content so that's why we don't have to weed out other content type URLs.

page.tpl.php
region.tpl.php
block.tpl.php
taxonomy-term--event-tags.tpl.php
My Local events...
taxonomy-term--event-tags.tpl.php

node--event.tpl.php
Local Event 1
Summary...
node--event.tpl.php

node--event.tpl.php
Local Event 2
Summary...
node--event.tpl.php

node--event.tpl.php
Local Event 3
Summary...
node--event.tpl.php
block.tpl.php
region.tpl.php
page.tpl.php

The taxonomy-term--event-tags.tpl.php template is only necessary if you need to add content above or below your node instances. Otherwise you will just use the page.tpl.php and node templates.


*If you do not see the social network icons here, it is because you have an ad-blocker running. Deactivate it for this site if you want to share this link. It is safe. There are no ads on this site.

This documentation is a work-in-progress. As I continue to learn more about the software, I will try to keep this documentation updated. I'm no expert of anything, so if anyone feels they need to correct me about something I've written or wish to add some additional tips, feel free to mention it in the comments.

"Drupal 7 Theme from Scratch" was written by TenTen71 because no one else would.