2

I have a custom node field I want to add into the page title. How can I do this?

Content type is a property listing and I want to have area size and location printed next to the title.

e.g. Large Mansion 900 sq ft (Chicago)

Location is also a taxonomy term in a dropdown list.

Do I need a preprocess_page function to extract the field and then use page.tpl.php?

2 Answers 2

4

A hook_preprocess_page function in your template.php file would certainly be one way to do it:

function MYTHEME_preprocess_page(&$vars) {
  $node = menu_get_object();
  if (isset($node->nid) && $node->type == 'article') {
    $data = $node->field_custom_field[$node->language][0]['value'];

    $vars['title'] = $vars['title'] . ' ' . $data;
  }
}

In page.tpl.php your $title variable should contain your new string based on the field.

You'll probably want to do a couple of extra checks in there as well, for example making sure the node is of the right type, etc.

EDIT

To address your comments I guess you'd want to do something like this:

$area_size = $node->field_area_size[$node->language][0]['value'];
$location = $node->field_location[$node->language][0]['value'];

$vars['title'] = "$vars[title] $area_size sq ft ($location)";
9
  • Thanks for your answer, I understand now. Can you tell me how I would do it for a certain content type? I want to grab a $node->custom_field in a content type and change the page title.
    – chrisck
    Mar 3, 2012 at 14:43
  • 1
    @Chris I've added an extra bit to the if statement, you'll just need to change the bit inside the quotes to match the name of your content type
    – Clive
    Mar 3, 2012 at 14:54
  • Thanks so much for that. Is there a way to keep the original title + field? I think that may be tricky.
    – chrisck
    Mar 3, 2012 at 15:00
  • 1
    You can just use the original title and tack it on the end, I've updated the answer again :)
    – Clive
    Mar 3, 2012 at 15:15
  • Thanks for the code. Here is mine: function MYTHEME_preprocess_page(&$variables, $hook) { $node = menu_get_object(); if (isset($node->nid) && $node->type == 'property_listing') { $variables['title_suffix'] = field_view_field('node', $node, 'field_area_size'); } } This works for me but I would like to add one more field onto the end of the 'field_area_size'. When I try add it on, I just get ArrayArray
    – chrisck
    Mar 4, 2012 at 2:50
1

There's a module for that: Page Title.

This module gives you granular control over the page title. You can specify patterns for how the title should be structured and, on content creation pages, specify the page title separately to the content's title.

1
  • 1
    I believe the Page title only works for <title> tags in the <head>.. not the <body>.
    – chrisck
    Mar 4, 2012 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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