将导航菜单css类添加到正文

时间:2012-12-09 作者:dvd3141

我想将当前菜单项的CSS类(及其父类和父类)添加到当前页面主体类中。

例如,如果当前菜单项有一个CSS类“products”(如外观|菜单中所定义),那么相应页面的主体也应该有一个类“products”。

如果这对任何人都有意义的话,有什么办法吗?

我已经看过了“How to add current, parent, and ancestor menu item IDs to body_class()?“但在我的情况下,这些想法是行不通的。

3 个回复
SO网友:dvd3141

多亏了阿加瓦尔先生的提示,这就是我设法拼凑起来的:

function pa_assign_menu_class_to_body(){
  // \'main\' is the theme_location, set earlier using register_nav_menus()
  $menu_name = \'main\';
  $class_list = array();

  if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    $menu_items = wp_get_nav_menu_items($menu->term_id);

    // _wp_menu_item_classes_by_context() adds current, current_item_parent and current_item_ancestor to the appropriate arrays in the provided variable
    _wp_menu_item_classes_by_context( $menu_items );

    $classes = array();

    foreach($menu_items as $menu_item) {
      if ($menu_item->current == 1) {
        $classes[\'current\'] = $menu_item->classes;
      }
      if ($menu_item->current_item_parent == 1) {
        $classes[\'parents\'] = $menu_item->classes;
      }
      if ($menu_item->current_item_ancestor == 1) {
        $classes[\'ancestors\'] = $menu_item->classes;
      }
    }

    // create a one-dimensional array of unique classes
    foreach($classes as $class) 
      foreach ($class as $cls) $class_list[] = $cls;
    $class_list = array_values(array_unique($class_list));
  }
  // if, for some reason, we have no results, we need to assign a default class otherwise WordPress complains
  if (empty($class_list)) $class_list[] = \'default\';
  return $class_list;
}
add_filter( \'body_class\', \'pa_assign_menu_class_to_body\');
这个函数做得有点太多,因为它添加了所有menu-item-也键入类。我真正想要的是用户在WordPress菜单管理屏幕的CSS类字段中输入的自定义CSS类。任何关于如何只提取这些类的想法都是非常受欢迎的。

SO网友:Mridul Aggarwal

在答案中you linked, 执行var_dump($dosmenu); 就在线的上方_wp_menu_item_classes_by_context( $dosmenu );. 该变量应该已经用您想要的类初始化。

之后,您可以调整foreach 相应地循环(&A);将该函数用作过滤器body_class.

admin中设置的类存储为带有键的post meta_menu_item_classes 在调用中自动初始化wp_get_nav_menu_items

SO网友:Dadiocoleman

对上面发布的代码稍加修改,只获得自定义类。自定义类始终是第一项。

function pa_assign_menu_class_to_body(){
  // \'main\' is the theme_location, set earlier using register_nav_menus()
  $menu_name = \'main\';
  $class_list = array();

  if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    $menu_items = wp_get_nav_menu_items($menu->term_id);

    // _wp_menu_item_classes_by_context() adds current, current_item_parent and current_item_ancestor to the appropriate arrays in the provided variable
    _wp_menu_item_classes_by_context( $menu_items );

    $classes = array();

    foreach($menu_items as $menu_item) {
      if ($menu_item->current == 1) {
        $classes[\'current\'] = $menu_item->classes;
      }
      if ($menu_item->current_item_parent == 1) {
        $classes[\'parents\'] = $menu_item->classes;
      }
      if ($menu_item->current_item_ancestor == 1) {
        $classes[\'ancestors\'] = $menu_item->classes;
      }
    }

    // create a one-dimensional array of unique classes
    foreach($classes as $class) 
      foreach ($class as $cls) $class_list[] = $cls;
    $class_list = array_values(array_unique($class_list));
  }
  // if, for some reason, we have no results, we need to assign a default class otherwise WordPress complains
  if (empty($class_list)) $class_list[] = \'default\';
  //Gets first class if custom. If not, returns null.
  if ($class_list[0] != \'menu-item\') {
      $class_list_return[] = $class_list[0];
  } else {
      $class_list_return[] = null;
  }
  return $class_list_return;
}
add_filter( \'body_class\', \'pa_assign_menu_class_to_body\');
EDIT: 不过,此过滤器和原始过滤器将替换默认类。如果您只想添加它们。。。

function pa_assign_menu_class_to_body($classes){
  // \'main\' is the theme_location, set earlier using register_nav_menus()
  $menu_name = \'main\';
  $class_list = array();

  if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    $menu_items = wp_get_nav_menu_items($menu->term_id);

    // _wp_menu_item_classes_by_context() adds current, current_item_parent and current_item_ancestor to the appropriate arrays in the provided variable
    _wp_menu_item_classes_by_context( $menu_items );

    foreach($menu_items as $menu_item) {
      if ($menu_item->current == 1) {
        $classes[\'current\'] = $menu_item->classes;
      }
      if ($menu_item->current_item_parent == 1) {
        $classes[\'parents\'] = $menu_item->classes;
      }
      if ($menu_item->current_item_ancestor == 1) {
        $classes[\'ancestors\'] = $menu_item->classes;
      }
    }

    // create a one-dimensional array of unique classes
    foreach($classes as $class) 
      foreach ($class as $cls) $class_list[] = $cls;
    $class_list = array_values(array_unique($class_list));
  }
  // if, for some reason, we have no results, we need to assign a default class otherwise WordPress complains
  if (empty($class_list)) $class_list[] = \'default\';
  //Gets first class if custom. If not, returns null.
  if ($class_list[0] != \'menu-item\') {
      $class_list_return[] = $class_list[0];
  } else {
      $class_list_return[] = null;
  }
  return array_merge($classes, $class_list_return);
}
add_filter( \'body_class\', \'pa_assign_menu_class_to_body\');

结束