将类别名称添加到 body_class

Avatar of Chris Coyier
Chris Coyier

body_class 函数用于向 body 标签添加许多类,这些类包含有关当前显示的页面类型的信息。可能是出于样式目的。但出于某种原因,它不包含当前类别(或类别)的类,用于单个帖子。

这会添加该类别的“好”名称

add_filter('body_class','add_category_to_single');
  function add_category_to_single($classes) {
    if (is_single() ) {
      global $post;
      foreach((get_the_category($post->ID)) as $category) {
        // add category slug to the $classes array
        $classes[] = $category->category_nicename;
      }
    }
    // return the $classes array
    return $classes;
  }