【问题标题】:no "current_page_item" class being added to top level nav item没有“current_page_item”类被添加到顶级导航项
【发布时间】:2013-04-17 20:40:39
【问题描述】:

如果您转到www.lindysez.com 并单击顶级导航链接,您会注意到位置存在由稍暗的背景指示(与悬停状态相同)。这适用于除“提示和技术”之外的每个链接。

如果我深入了解,我注意到“current_page_item”类没有被添加到父类中

  • 转到http://www.lindysez.com/tips 时的“提示和技巧”菜单项

    另外,奇怪的是,当在提示页面上时,父母

  • “博客”导航项的
  • 获得了类“current_page_parent”,这既不是预期的,也不是我们想要的。从 UI 的角度来看,这是无害的,但可能表明潜在的问题可能是什么。

    有人知道为什么“提示和技巧”菜单项在选择后没有获得“current_page_item”类吗?


    更新:指示@user2019515 在下方发表评论

    感谢您在这个方向上的推动,看来我可以创建一个解决此问题的解决方法。然而,一些事情。

    1. 这会处理问题的症状而不是根源。我想弄清楚如何让 wp 直接添加“current_page_item”类。知道是什么原因造成的吗?我认为这也可能与我遇到的另一个问题有关,该问题涉及未在 RSS 提要中显示的“提示”。我发布了另一个关于此的问题,但还没有看到任何答案...... https://stackoverflow.com/questions/15980846/custom-post-type-not-showing-up-in-wordpress-rss-feed

    2. 即使作为一种解决方法,我也宁愿尽​​量避免依赖硬编码的“菜单项-##”的解决方案。我的开发和生产服务器都有不同的 ID#。我知道我可以解释这一点,但我希望有更好的、可扩展的解决方法。

    3. 您提供的代码对我不起作用,并不是说我可以从中找到任何错误,也没有任何错误。似乎它应该工作,但只是没有。我什至尝试创建一个非常简单的版本,为所有项目添加一个类,但这也没有执行。这是代码。

      function add_class_to_wp_nav_menu($classes, $item)
      {
          $classes[] = 'classy-class';
          return $classes;
      }
      add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu', 10, 2);
      

    非常感谢您的帮助!

  • 【问题讨论】:

      标签: wordpress-theming wordpress


      【解决方案1】:

      提示页面是一个帖子存档页面,它显示帖子而不是普通页面,这就是博客被突出显示的原因。您可以通过将以下自定义 CSS 添加到您的 style.css 文件中轻松解决此问题。

      .post-type-archive-tips .menu-item-23{
          background:url(images/nav-hover.png) repeat-x;
      }
      

      【讨论】:

      • 我认为这行不通。当我在提示页面上时,“提示和技术”链接的父
      • 有一个 class="menu-item menu-item-type-post_type menu-item-object-page menu-item-23"如果我在“食谱”页面上,这与它获得的课程完全相同。感谢您的尝试,我知道您将如何处理这个问题,但我真的很想弄清楚如何添加“current_page_item”类。 “提示”是自定义帖子类型,就像“食谱”一样,所以我认为它们的工作方式应该相同。
    • 我将在下面添加一个新答案。
    • 【解决方案2】:

      你需要遍历你的菜单,默认情况下当前页面类被添加到博客页面,因为最后是自定义帖子。

      //Change current_page_parent for custom post type
      function remove_parent_classes($class)
      {
        // check for current page classes, return false if they exist.
        return ($class == 'current_page_item' || $class == 'current_page_parent' || $class == 'current_page_ancestor'  || $class == 'current-menu-item') ? FALSE : TRUE;
      }
      
      function add_class_to_wp_nav_menu($classes)
      {
        switch (get_post_type()) {
          case 'portfolio':
            // we're viewing a custom post type, so remove the 'current_page_xxx and current-menu-item' from all menu items.
            $classes = array_filter($classes, "remove_parent_classes");
      
            // add the current page class to a specific menu item (replace ###).
            if (in_array('menu-item-14', $classes)) {
               $classes[] = 'current_page_parent';
            }
            break;
          case 'tips':
            // we're viewing a custom post type, so remove the 'current_page_xxx and current-menu-item' from all menu items.
            $classes = array_filter($classes, "remove_parent_classes");
      
            // add the current page class to a specific menu item (replace ###).
            if (in_array('menu-item-23', $classes)) {
               $classes[] = 'current_page_parent';
            }
            break;
        }
        return $classes;
      }
      add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
      

      该函数中只有一种自定义帖子类型,但我相信您可以弄清楚如何添加更多。有任何问题,请随时发表评论! :)

      【讨论】:

      • 我的评论太长,无法作为评论留下,因此我将使用此信息更新原始案例。请看上面
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签