Home > Tools > CMS > Drupal > Drupal Hacks > How to Optimize Views 2 More Links Anchor Texts

How to Optimize Views 2 More Links Anchor Texts

One 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 option Views 2 Block settings: Link display option

The 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:

  1. Copy views-more.tpl.php from PATH_TO_VIEWS_MODULE/theme/ to your theme directory
  2. Replace the content of views-more.tpl.php in your theme directory with the code below.
  3. Clear the theme cache. You can achieve this by clicking on Clear cached data at the bottom of the Performance settings form on the admin/settings/performance page. This will clear all cached data, which should be avoided on high traffic sites. Alternatively you can call drupal_rebuild_theme_registry() in your template.php file once after you copied and edited the views-more.tpl.php file or by executing this SQL query
    DELETE 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 ;-)

I'm assuming that yes the menu title should be treated like any other string and run through check_plain before being displayed. It would not have been done when the menu form was submitted since we don't know the final context of the data there is no way to know how/what to filter. Though perhaps this is a special case since menu titles get stored to the menu table and then again to the router table. It certainly can't hurt to use check_plain here. For the definitive information on safe strings see Steven Witten's article entitled "Safe String Theory for the Web" http://acko.net/blog/safe-string-theory-for-the-web

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.

Thanks, this is a useful tip.
Nice information
Yes Good SEO Tips but Still Required more point as SEO IS Concern ….However Thanks For Sharing with us..

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br> <img> <h2> <h3> <h4> <h5>

More information about formatting options