How to Optimize Views 2 More Links Anchor Texts
Posted: Wed 10/08/2008 by ramiroOne of the great improvements in Views 2 is the ability to create various page and block displays for one view. Block views can be linked to a page display when the More link option in the block's settings is enabled.
If there are one or more page displays set up in the view, the page to be linked can be chosen after clicking on the link next to the Page display option. See the screenshot below for an example:
Views 2 Block settings: Link display optionThe default anchor text of more links in block views is more which comes with the benefit to be pretty generic but is not good from an SEO point of view.
Anchor texts are very important for search engine rankings as the number one search result for click here demonstrates. Neither the words click nor here actually occur on the Adobe Reader download page, but it is linked with this anchor text very often.
To replace the default anchor text for more links with the title of the linked page, which is hopefully more meaningful, you can override the views-more.tpl.php template file by taking the following steps:
- Copy
views-more.tpl.phpfromPATH_TO_VIEWS_MODULE/theme/to your theme directory - Replace the content of
views-more.tpl.phpin your theme directory with the code below. - Clear the theme cache. You can achieve this by clicking on
Clear cached dataat the bottom of the Performance settings form on theadmin/settings/performancepage. This will clear all cached data, which should be avoided on high traffic sites. Alternatively you can call drupal_rebuild_theme_registry() in yourtemplate.phpfile once after you copied and edited theviews-more.tpl.phpfile or by executing this SQL queryDELETE FROM `cache` WHERE cid LIKE 'theme_registry%';
Code in views-more.tpl.php
<?php
$offset = strlen(base_path());
$path = substr($more_url, $offset);
$menu_item = menu_get_item( $path );
$title = check_plain($menu_item['title']);
?>
<div class="more-link">
<a href="<?php print $more_url; ?>">
<?php print $title; ?>
</a>
</div>The $more_url variable is made available by the views module in the views-more.tpl.php template file. It contains the absolute path to the linked page view. To get the title of the linked page, the internal Drupal path is extracted from $more_url using PHP's substr() function.
The internal Drupal path is needed to call menu_get_item(), which returns a menu router item that in turn contains the page title in the value for the title array key.
Actually, I am not sure whether calling check_plain() on the title is necessary here, since I assume that this taken care of before the router item is written to the database. Any insight on that is greatly appreciated.
The remainder of the code in the template file consists of the mark up of the more link with the linked page title as the anchor text.
That's it. A simple yet effective method of optimizing the anchor texts of more links in Views 2 blocks. A demonstration of this code in use can be seen in various blocks on linux-netbook.com.
The screenshot of the views settings page and the annotations are done with the awesome Firefox extension FireShot, that I just discoverd today. There is great software outside the Drupal universe ;-)
- Login to post comments

After skimming through menu.inc I also conclude that it's best to call
check_plain(). Since module author's can define custom "title callback" functions, one cannot be certain, that the title string is safe for display.