TransWikia.com

How can I change the front-page title when front page is not /node?

Drupal Answers Asked on December 12, 2021

I changed the site’s front page from /node to another page. However, now the title of the front page is page name | site name. To clear up any confusion, by "title," I mean the phrase entered between the HTML <title> tags.

I tried this SO answer, but it had no effect. How can I declaratively change the name of the site’s main page?

4 Answers

It's not possible to change the front page title declaratively without adding a new module, such as the Metatag module. Drupal's core does not have this functionality. However, you can achieve this with a few lines of code implementing template_preprocess_html in your custom theme.

To achieve this dynamically in a subtheme, add this code to your preprocess_html function of your theme file or add if it doesn't exist.

function MYTHEME_preprocess_html(&$variables) {
  if (Drupal::service('path.matcher')->isFrontPage()) {
    $variables['head_title'] = [t('Hello World')];
  }
}

Note: Credit to @leymanx for the basis for the above code, which just needed a slight modification to update the title as originally asked, rather than just the portion of the before name of the site.

Answered by glenviewjeff on December 12, 2021

The default Frontpage is a view (admin/structure/views/view/frontpage/edit) showing a waterfall of all content that is Published and "Promoted to the Frontpage". By default, the Frontpage view's Title is "none" while the HTML Title tag is "Home | MySite.com".

So you can change the Title of the view there to e.g. "Start" and the HTML title will change to "Start | MySite.com".

If, instead of using the Frontpage view, you choose a node for your home page (e.g. "node/10 ), then you can name the page "Home" and the HTML title will become "Home | MySite.com".

If you don't want the title of the page showing up in the page's content area (but you do want it in the home page's HTML title tag), you can hide the title block in admin/structure/block/manage/yourtheme_page_title.

The Metatag module, along with Token, can also be used for this and general SEO enhancements, but you said you are trying to avoid that.

Answered by edg on December 12, 2021

You can achieve this with a few lines of code implementing template_preprocess_html in your custom theme.

/**
 * Implements template_preprocess_html().
 */
function MYTHEME_preprocess_html(&$variables) {
  if (Drupal::service('path.matcher')->isFrontPage()) {
    $variables['head_title']['title'] = t('Hello World');
  }
}

Answered by leymannx on December 12, 2021

You can use the Metatag module. It has a section for front page metatags, where you can set the title.

Answered by Jaypan on December 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP