Change Title On Blog Index Page In Drupal

Tuesday, May 11, 2010 - 23:37

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.

1
2
3
4
5
6
7
8
9
10
/**
 * 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.

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

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.
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! :)

Thanks dude!!

I have been dealing with this problem for so long!

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!

philipnorton42's picture
Submitted by philipnorton42 on Fri, 04/08/2011 - 15:14

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.

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

</p><p>including the page title in another way.</p><p>Thanks anyway!</p><p>&nbsp;</p>

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?

This a great post! I enjoyed reading it.

I think this article could complement your post.

Good luck with your future piece :)

Add new comment