Drupal page titles like breadcrumbs

Filed under:  Google about "Drupal" |  Google about "Search engines"

The page title is one of the most important pieces of information that search engines display on their results page, for any given search query. Choosing the right title for your pages, articles (or posts of any type) is probably a key factor that may lead users to choose your site ...or just jump to somewhere else.

By default, Drupal displays the site name and the page title, which is nice. However, any given page title you need to write is probably based on the context where it is published. So that alone might be misleading if it is not seen within the correct context. That is, in some circumstancies (say a book page, taxonomy based reports, etc.) only the title might not be enough.

On the other hand, Drupal has another pretty nifty feature, the ability to build breadcrumbs based on context. That's almost perfect, for what I was trying to accomplish. All we need to do is build the page title based on the current active breadcrumb and append the page title.

I thought I could share the code snippet I'm using on this site, so you may try it and see if such a small change can be translated into more visits coming from search engine queries.

Edit the file page.tpl.php of your theme and find a line similar to the following:

<title><?php print $head_title </title>

Just before that line, append the following code, save, upload and you're done.

<?php
$_items 
drupal_get_breadcrumb();
if (
is_array($_items)) {
    foreach (
$_items as $i => $_item) {
        
$_items[$i] = preg_replace('#<a.*?>(.*?)</a>#''\1'$_item);
    }
    
$_items[0] = variable_get('site_name''drupal');
    if (!
$is_front && drupal_get_title()) {
        
$_items[] = strip_tags(drupal_get_title());
    }
    else if (
$is_front && variable_get('site_slogan''')) {
        
$_items[] = variable_get('site_slogan''');
    }
    
$head_title implode(' &rsaquo; '$_items);
}
?>

I hope you like it. ;-)


some characters got mixed up?

The regexp doesn't seem quite right...
Let's see if the regexp stays intact in the comments:

preg_replace('#<a.*?>(.*?)</a>#', '$1', $_item);

Great snippet otherwise, and great idea!


Good catch!

Oops, I didn't noticed the ?> part was missing.

Thanks and glad you liked it!