Body class (wordpress)

Add custom classes to array of body classes.

<?php
/**
 * Add custom classes to array of body classes.
 */
add_filter('body_class', function($classes) {
  $classes[] = class_exists('Classic_Editor') ? 'classic-editor' : 'block-editor';
  if (!is_singular() && !is_404()) {
    $classes[] = 'hfeed';
  }
  if (is_singular() && !is_front_page()) {
    $post_type = get_post_type();
    if ($post_type && 'post' !== $post_type) {
      $slug = get_post_field('post_name', get_queried_object_id());
      if (!empty($slug) && !in_array($post_type.'-'.$slug, $classes)) {
        $classes[] = $post_type.'-'.$slug;
      }
      if ($ancestors = get_post_ancestors(get_queried_object_id())) {
        foreach ($ancestors as $ancestor) {
          if ($page = get_post($ancestor)) {
            $classes[] = 'ancestor-'.$page->post_type.'-'.$page->post_name;
          }
        }
      }
    }
  }
  return $classes;
});
?>

functions.php

< toolbox