[Drupal] How to enable dynamic page titles in a Drupal 6 website?
You might wish to have dynamic page titles in your Drupal 6 website. It is good for a website for the title to change according to the content of a page from an SEO perspective and also for a better content structure. It is very easy to implement dynamic page titles in a Drupal 6 website. Follow the steps below to change the page title according to the url of the page.
Where should we place the code?
The code for the dynamice page titles should be placed in the theme file or to be more specific in the template.php file of your theme. The code should be placed in the preprocess_page function inside the template.php file. Now we are going to implement this based on the url of the page, which means that the title will change based on the url of the website.
The template.php file
Here is a good sample code for you to work on your own Drupal website to implement dynamic page titles
function mytheme_preprocess_page(&$vars) {
if(arg(0) == '' && arg(1) == NULL) {
$vars['head_title'] = " Home | Great Website ";
}
if (arg(1) == 'drupal-great') {
$vars['head_title'] = " Drupal is Awesome | Great Website";
}
}
Here arg(0) is the variable for the first part of the url after the main site name and arg(1) is the next part of the url separated by the slash character '/'. In this case if the url is www.site-name/home
the title would be " Home | Great Website " and if the url is www.site-name/home/drupal-great
the title would be " Drupal is Awesome | Great Website ".
The page.tpl.php file.
Now that was the first part. If you had looked at the code above, you would have noticed that that the title was assigned to a url, more specifically the 'head_title' variable. This variable should be printed within your page.tpl.php file of your theme.
<title><?php print $head_title; ?></title>
It should be printed inside the HTML title tags so as for the browser to recognize it as the page title.