3

I'm trying to use drupal_set_title() in my node.tpl.php but the text I'm setting the title to is only showing up in the $head_title (html.tpl.php) variable and not the $title variable. The $title variable is still set to the node's title.

This is in Drupal 7. I've used drupal_set_title() multiple times like this in Drupal 6 and it has worked perfectly.

Is this broken in D7?

This is how it is being used in node--news.tpl.php:

$actual_title = $title; // save for later use
drupal_set_title('News');
1
  • Could you please show us the code showing how you are using it?
    – Ashlar
    Jan 11 '12 at 0:23
10

By the time you've hit node.tpl.php it's too late to set the title as $head_title has already been set in template_process_html() and the $head_title variable has already been output.

You'll need to use template_preprocess_node() or similar to set your title as desired.

2

Apparently trying to set the title in node.tpl.php is too late in the execution order. Same goes for template_preprocess_node(). Instead, it can be set in template_preprocess_page:

function mytheme_preprocess_page(&$vars){
    if (isset($vars['node']) && $vars['node']->type == 'news'){
        drupal_set_title('News');
    }
}
1
  • it's poor form to reiterate the same answer Dave gave but mark yours as the accepted answer when asking a question. Please change. Mar 15 '14 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.