Published on

Using a field as page title with the Metatag module for Drupal

Drupal

For SEO, the title of a page can be one of the elements to optimize in order to target certain requests or simply to give more information to search engines about the content present on a page.

For this site, I created a Technologies taxonomy in order to communicate range of services that I offer to potential prospects. These technologies are referenced everywhere else on the site, so it made sense to use their name as a page title in order to be able to display it in lists, tooltips, search, etc.

Creating a new Title field

However, by titling my page Drupal (for example), neither search engines nor users understand that it is a page that presents my skills and not a generic page presenting Drupal as such.

So I created a new Title field associated with this taxonomy term in addition to the default Name field. This Title field will be the h1 title displayed as the page title.

This Title field is then used in Twig templates in place of the initial Name field.

<h1>{{ content.field_technology_title }}</h1>

Changing the page title in the head tag

To perfect this implementation, I also changed the title of the page via the Metatag module by using a token of the type [term:field_title] in the title field.

Thanks to this change:

  • My urls are clean (like /technologies/drupal)
  • I can reference technologies elsewhere on the site and only display their name (at the top of this page for example)
  • I can define an SEO-Friendly page title sush as "Senior Drupal developer in Strasbourg" which is displayed correctly on the page, in the head tag thanks to the Metatag module and therefore in the page tab.