Drupal 6: Change Title On Blog Index Page In Drupal

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

Exactly what I was looking for thanks! This was the only page title that I wasn't able to edit with the page title module; super annoying.
Permalink
Another route that doesn't require any code is to use the String Overrides module, and specify "Blogs" as the Original, and "Blog" (or whatever) as the Replacement. Just another thought! :)
Permalink

Thanks dude!!

I have been dealing with this problem for so long!

Permalink

Hi

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!

Permalink

It 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.

Name
Philip Norton
Permalink

Probably you are right, but can be too long to test. I fixed with a

including the page title in another way.

Thanks anyway!

 

Permalink

I 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?

Permalink
This a great post! I enjoyed reading it. I think this article could complement your post. Good luck with your future piece :)
Permalink

I 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.

Permalink

I really appreciate your work and very amazing and important information about the drupal which have share in this blog.

Permalink

Thanks!! Exactly what i was looking for..

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.