Define custom twig page template for specific multiple node pages by nid - page--custom.html.twig

Solutions

This page describes everything about naming conventions, twig template naming conventions.

As described it is possible to do like this: node--nodeid.html.twig, e.g node--2.html.twig.

The smartest way will be to implement the hook_theme_suggestions_page_alter(), like this:

/**
 * Implements hook_theme_suggestions_page_alter().
 */
function THEMENAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $nodes = [2, 4, 20, 40];
    if (in_array($node->id(), $nodes)) {
      $suggestions[] = 'page__custom';
    }
  }
}

You can write a hook_theme_suggestions_page_alter()

/**
 * Implements hook_theme_suggestions_page_alter().
 */
function YOUR_THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    if ($node->id() == 2 || $node->id() == 4 || $node->id() == 20 || $node->id() == 40) {
      $suggestions[] = 'page__custom';
    }
  }
Tags: Drupal 8 / Theming / Hooks

Similar questions

How filter node inside view block by even nid (or odd nid)
I have a view 2 blocks where I show some images and I'm trying to show the images with an even nid in the 1st block and the images with odd nid in the 2nd block Do you guys have any idea if it's possible? and how?
Use page.html.twig template variable in another twig template
How do I call a page.html.twig template variable from the book-tree.html.twig template. Using kint, I have found the variable, nid as in the screenshot below: The 'nid' is highlighted, but it is the 'x-default value that is required. This variable will be used in a set statement, comparing it to the item.original_link.nid to set an active class. Th...
How do I define a template file for a specific node ID?
I'm trying to define a custom template file for a specific node, using the node ID. I read a lot of topics saying to create a node--X.tpl.php file in the templates folder. That's what I did; unfortunately, even after flushing the theme cache many times, nothing changes. Is there anything more I have to do?
How do I define a template file for a specific node paragraph ID?
I would like the ability to override a template for only 1 paragraph. Is it possible to override just one paragraph like we can with nodes node-{nodeid}.html.twig?
Understanding page-title.html.twig and/vs field--node--title.html.twig
This question is based on the desire to have only one wrapping tag around the title value. (I am utilizing the block system to render the page/node title) Both of the templates utilize some sort of attribute array on the wrapping element, so I am not sure it is safe to remove either. I am hoping to better understand why there are two levels to this...
"Twig_Error_Loader: Template "file.html.twig" is not defined" for new twig template file
After creating a new paragraph type and its template file in the correct folder location, I am receiving the following error every time I view the page with that houses the new paragraph type. Twig_Error_Loader: Template "file.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "file.html.twig" in the...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.