2015-03-16 21:18:03 +00:00
|
|
|
|
---
|
|
|
|
|
title: Style Drupal 6's Taxonomy Lists with PHP, CSS and jQuery
|
2020-03-08 14:32:13 +00:00
|
|
|
|
date: 2010-04-05
|
2018-12-31 12:13:05 +00:00
|
|
|
|
excerpt: Getting started with Drupal theming by styling Drupal’s taxonomy lists.
|
2015-03-16 21:18:03 +00:00
|
|
|
|
tags:
|
2015-06-14 02:27:41 +00:00
|
|
|
|
- drupal-6
|
|
|
|
|
- drupal-planet
|
|
|
|
|
- drupal-theming
|
|
|
|
|
- taxonomy
|
2015-03-16 21:18:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
Whilst developing this, and other Drupal websites for clients, I decided that I
|
|
|
|
|
wanted to categorise content using the taxonomy system. However, I wasn't happy
|
|
|
|
|
with the way that Drupal displayed the terms lists by default, and I started
|
|
|
|
|
comparing this to other websites that I look at.
|
|
|
|
|
|
|
|
|
|
To start with, I wanted to have something that described what the list was
|
|
|
|
|
displaying - like in the second example above. I wanted to have the words
|
|
|
|
|
'Posted in' displayed before the list of terms. To do this, I had to edit the
|
|
|
|
|
node template file that exists within my theme folder (sites/all/themes). As I
|
|
|
|
|
only wanted this change to affect my Blog posts, the file that I needed to
|
|
|
|
|
change is **node-blog.tpl.php**
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
|
|
|
|
I scrolled down until I found the piece of code that displayed the terms list:
|
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```language-php
|
2015-03-16 21:18:03 +00:00
|
|
|
|
<?php if ($terms): ?>
|
2015-06-14 02:27:41 +00:00
|
|
|
|
<div class="terms terms-inline">
|
|
|
|
|
<?php print t('Posted in') . $terms; ?>
|
|
|
|
|
</div>
|
2015-03-16 21:18:03 +00:00
|
|
|
|
<?php endif; ?>
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
Adding `print t(' Posted in ')` will print the words 'Posted in' before
|
|
|
|
|
outputing the terms.
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
I then added some CSS to re-size the spacing between the items, and then add the
|
|
|
|
|
commas between them to seperate them:
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```language-css
|
2015-03-16 21:18:03 +00:00
|
|
|
|
.terms ul.links li {
|
2015-06-14 02:27:41 +00:00
|
|
|
|
margin-right: 1px;
|
|
|
|
|
padding: 0;
|
2015-03-16 21:18:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.terms ul.links li:after {
|
2015-06-14 02:27:41 +00:00
|
|
|
|
content: ",";
|
2015-03-16 21:18:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.terms ul.links li.last:after {
|
2015-06-14 02:27:41 +00:00
|
|
|
|
content: ".";
|
2015-03-16 21:18:03 +00:00
|
|
|
|
}
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
I created a file named **script.js** in my theme folder with the following code
|
|
|
|
|
in it. After clearing Drupal's caches, this file is automatically recognised by
|
|
|
|
|
Drupal 6.
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```language-js
|
2015-03-16 21:18:03 +00:00
|
|
|
|
if (Drupal.jsEnabled) {
|
2015-06-14 02:27:41 +00:00
|
|
|
|
$(document).ready(function() {
|
|
|
|
|
$('.terms ul.links li.last').prev().addClass('test');
|
|
|
|
|
})
|
2015-03-16 21:18:03 +00:00
|
|
|
|
}
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
2020-03-08 17:52:59 +00:00
|
|
|
|
This code finds the last item in the list, uses **.prev** to select the one
|
|
|
|
|
before it, and then uses **.addClass** to assign it the HTML class of "test". We
|
|
|
|
|
can then use this class to target it with specific CSS.
|
2015-03-16 21:18:03 +00:00
|
|
|
|
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```language-css
|
2015-03-16 21:18:03 +00:00
|
|
|
|
.terms ul.links li.test:after {
|
2015-06-14 02:27:41 +00:00
|
|
|
|
content: " and";
|
2015-03-16 21:18:03 +00:00
|
|
|
|
}
|
2017-03-16 08:09:52 +00:00
|
|
|
|
```
|