4

I was wondering how I would change page.html.twig depending on the content type of the node I am viewing?

For example I would like the title to be shown on pages with the article content type but be hidden on the site page content type.

1 Answer 1

5

Page title is available as a block, and you can control the visibility from there.

However, to answer your question, here's how to provide different template suggestions based on content type:

function YOUR_THEME_theme_suggestions_page_alter(array &$suggestions, array $variables, $hook) {
  // Try to get the node, so we can check if we're on the node page.
  $node = Drupal::request()->attributes->get('node');
  if ($node && $node->getType() == 'YOUR_CONTENT_TYPE') {
    $suggestions[] = 'page__NAME_OF_YOUR_TEMPLATE';
  }
}

This will allow you to use page--NAME-OF-YOUR-TEMPLATE.html.twig template in your theme only for specific content types.

You can read more about hook_theme_suggestions_HOOK_alter() here.

Your Answer

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

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