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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityPage 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.