Last updated September 26, 2009. Created on September 26, 2009.
Edited by quantumized. Log in to edit this page.

When outputting groups of items in a view it may be necessary to automatically create element ID names for bookmark purposes. For example, when creating long Views lists, you may want to have sub-items jump directly to an item in your list. To do this you will need to have a name id in your anchor tag. By modifying your Views output template you can easily achieve this.

First, locate the appropriate Views output template and copy it into your current theme folder (Instructions). The code for this example was used in views-view-unformatted.tpl.php:

Modify the title output in the template to something similar to:

<?php if (!empty($title)): ?>
<?php $words = str_word_count($title, 2);?>
<?php $bookmark = implode('-', array_slice($words, 0, 2));?>
  <h3 id="<?php print strtolower($bookmark);?>"><?php print $title; ?></h3>
<?php endif; ?>

This will output something like:

<h3 id="bookmark">Title</h3>

Looking for support? Visit the forums, or join #drupal-support in IRC.


ar-jan’s picture

This was informative, as is #1078962: Views results grouped by first letter + a special group [0-9] + Alpha Pager made with anchors .
I'm not sure if you could avoid overriding a Views template in all situations, but I ended up using this:
A taxonomy term field, used as the group by item. I then used "Rewrite the output of this field", and put in <a name="[name]">[name]</a>.