Add Taxonomy ID class to the body tag (D8)
The linked example gets the path. Here better use routing. Check the name of the matched route and then get a parameter which is defined in the route. Normally you use getParameter() to get the loaded entity, in this case it's getRawParameter() for the id:

mytheme.theme:

function mytheme_preprocess_html(&amp;$variables) {
  if (\Drupal::routeMatch()-&gt;getRouteName() == 'entity.taxonomy_term.canonical') {
    $variables['term_id'] = \Drupal::routeMatch()-&gt;getRawParameter('taxonomy_term');
  }
}


html.html.twig:

{%
  set body_classes = [
    term_id ? 'term-id-' ~ term_id,
  ]
%}
&lt;body{{ attributes.addClass(body_classes) }}&gt;