The title tag on the Blog module index page (found at /blog after the module is installed) is by default "Blogs | Sitename", which isn't editable in the backend of Drupal. I've been talking to other Drupal developers and reading forums about the best way to go about changing the title of the blog index page. Ideas ranged from editing or duplicating the blog module (which is bad practice) to installing the String Overrides to provide a quick translation of the string in the title.
These suggestions are either quite poor, or simply overkill for what should be a simple string replacement. There were two methods we decided upon that work quite well and are easy to implement.
1) The first involves creating a page-blog.tpl.php template file and adding a str_replace() function call to replace the word blog from the title of the page. This would look something like this:
<title><?php print str_replace('Blogs | Sitename', 'Blog | Sitename', $head_title); ?></title>
I have entered the entire site name here as this cuts down on changing the title on legitimate blog posts containing the word "blog". The only limitation here is that this will need to be altered every time the title changes. This, however, shouldn't be very much and so is fairly robust solution on most cases. A few false positives might occur in the future with some blog titles, but these should be minimal.
2) The second method involves the use of the page variable $vars['head_title'] in the template.php file, which changes the $head_title variable before it gets to the template. This is done by using the hook_preprocess_page() hook and checking the value of arg(0) to make sure it is 'blog', which will be the case only on the blog index page. Create the following function in your template.php file in your theme and rename it so that it is inline with your own theme name.
/**
* Override or insert variables into the templates.
*
* @param array $vars The array of variables to pass tothe the theme template.
*/
function theme_preprocess_page(&$vars, $hook) {
if (arg(0) == 'blog') {
$vars['head_title'] = str_replace('Blogs', 'Blog', $vars['head_title']);
}
}
The arg(0) function will only ever return 'blog' if we are looking at the blog main page. So a simple find and replace here is enough to change the title and won't effect any blog post title. It might also be worth checking that the length of the array returned by arg() is 1 to make absolutely certain that this is the index page.
Which method you chose here depends on your own preferences, but I think that the second is more robust and maintainable than the first. The second method also doesn't require a separate blog page template, which is pointless if you are only changing a single variable.
There might also be other ways in which the title tag can be changed. If you know one and want to share it with us then post a comment.
Update: This post is specifically for Drupal 6 and so might not work for other versions of Drupal.
Comments
Submitted by Ben Leivian on Thu, 05/20/2010 - 00:38
PermalinkSubmitted by Adam Fairhead on Sat, 09/25/2010 - 16:17
PermalinkThanks dude!!
I have been dealing with this problem for so long!
Submitted by Raúl on Wed, 03/23/2011 - 11:56
PermalinkHi
Exactly what I was looking for, but is not working on my site: Drupal 6.19, acquia marina theme 3.1
I actually tested both with function acquia_marina_preprocess_page and function acquia_preprocess_page, cache cleared and so on.
Any ideas?
Thanks anyway!
Submitted by Mauri on Fri, 04/08/2011 - 13:54
PermalinkIt might be being overridden by the parent theme fusion_core? Other than that I'm not sure. You could always try putting a die('test'); in the preprocess_page hook to see that it is being called.
Submitted by giHlZp8M8D on Fri, 04/08/2011 - 14:14
PermalinkProbably you are right, but can be too long to test. I fixed with a
Submitted by Mauri on Thu, 04/14/2011 - 09:37
PermalinkI tried this in D7 using a sub-theme of Zentropy & saw no change (yes, I flushed the cache). What I really need to change is the default page title display (e.g., the H1 title) & the corresponding breadcrumb on /blog and /blog/1 (but not /blog/1/*). Tried changing the variable to just plain "title"; also looked at the blog module to see if there was anything there I could override in template.php, but no joy. Any suggestions?
Submitted by seezee on Thu, 10/13/2011 - 17:21
PermalinkThis tutorial is for Drupal 6, which is why things are probably not working. There also might be an issue with the sub theme you are using.
Try using drupal_set_title() to change the title. You just need to correctly detect the right page before setting it.
http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/drupal_set_title/7
Submitted by giHlZp8M8D on Thu, 10/13/2011 - 17:28
PermalinkSubmitted by shreelatha on Mon, 05/22/2017 - 07:19
PermalinkI actually tested both with function acquia_marina_preprocess_page and function acquia_preprocess_page in my site this is working properly.
Thank you for this post. A very helpful one.
Submitted by Cloudi5 on Fri, 07/10/2020 - 10:09
PermalinkI really appreciate your work and very amazing and important information about the drupal which have share in this blog.
Submitted by Cloudi5 on Wed, 12/16/2020 - 12:23
PermalinkThanks!! Exactly what i was looking for..
Submitted by aqsa on Thu, 06/03/2021 - 10:55
PermalinkAdd new comment