自定义仪表板菜单不会为其中的自定义分类保持打开状态

时间:2013-04-24 作者:Radley Sustaire

简短版本:

具有支持自定义分类法的自定义帖子类型的自定义菜单。自定义post类型有效,其菜单有效并保持打开状态。自定义分类法可以工作,但侧栏菜单不能保持打开状态。我需要使类别保持子菜单的打开状态,就像它在本机上一样(如果没有放置在另一个菜单中)。

长版本:

我有一个使用add\\u menu\\u页面创建的自定义菜单:

add_menu_page(
  \'Directory\',           // Page Title
  \'Directory\',           // Menu Title
  \'manage_options\',      // Capability
  \'directory\',           // Slug
  null,                  // Rendering Function
  \'div\'                  // Menu Icon (or "div" for CSS)
);
我还创建了一个自定义帖子类型和一个自定义分类法。我想让它们出现在我的自定义菜单中,根据show_in_menu 参数(请参见wiki,无法发布链接),这应该是本机支持的。

然而,它只是does not work correctly. 确实,此选项会将“管理目录”链接粘贴到我的自定义菜单中,但它确实not 将“添加联系人”或“目录类别(自定义分类)”链接移动到菜单中。

以下是一些预览:

自定义菜单:http://radleygh.com/images/chrome_2013-114-14-14-16-91.png

默认菜单:http://radleygh.com/images/chrome_2013-114-14-14-53-25.png

如您所见,默认菜单有三个链接。我得走了all of these 到我的自定义菜单(我将在其中添加子菜单)。

我之前学到的一个技巧是,可以为现有页面创建子菜单,它们的行为就像是真正的菜单一样。至少,这适用于大多数情况。它不适用于我的分类法。请参见以下代码:

add_submenu_page( // Manage Directory
  \'directory\',
  \'Manage Directory\',                  // Page Title
  \'Manage Directory\',                          // Menu Title
  \'manage_options\',                    // Capability
  \'edit.php?post_type=directory_post\'  // Slug
);
add_submenu_page( // Add Contact
  \'directory\',
  \'Add Contact\',                            // Page Title
  \'Add Contact\',                            // Menu Title
  \'manage_options\',                         // Capability
  \'post-new.php?post_type=directory_post\'   // Slug
);
add_submenu_page( // Categories
  \'directory\',
  \'Categories\',                             // Page Title
  \'Categories\',                             // Menu Title
  \'manage_options\',                         // Capability
  \'edit-tags.php?taxonomy=directory_category&post_type=directory_post\'  // Slug
);
上面的代码为我提供了所需的链接,每个链接的“slug”足够聪明,可以指向正确的页面。事实上,这些链接似乎是本地链接。但当然,他们不是。

但问题是,出于某种原因,“类别”链接本身并不能保持菜单的打开状态。其他人是这样的。

我认为问题在于分类法不支持“show\\u in\\u menu”参数。尽管正如我上面解释的那样,这个参数是“坏的”,但它至少保持了菜单的打开状态。

以下是目录帖子类型和分类的完整定义

// Register the post type and custom category (aka taxonomy)
function dir_register_directory() {
  $args = array(
    \'labels\' => array(
          \'name\' => \'Directory Listings\',
          \'singular_name\' => \'Directory Listing\',
          \'add_new\' => \'Add Contact\',
          \'add_new_item\' => \'Add New Contact\',
          \'edit_item\' => \'Edit Contact\',
          \'new_item\' => \'New Contact\',
          \'all_items\' => \'Manage Directory\',
          \'view_item\' => null, // Don\'t need to view these
          \'search_items\' => \'Search Directory\',
          \'not_found\' =>  \'No contacts found\',
          \'not_found_in_trash\' => \'No contacts found in Trash\', 
          \'parent_item_colon\' => \'\',
          \'menu_name\' => \'Directory\'
        ),
    \'capability_type\'     => \'post\',
    \'hierarchical\'        => false,

    \'public\'              => true,
    \'show_in_menu\'        => \'directory\', // Nest the link under custom menu
    \'show_in_nav_menus\'   => false, // Do NOT show these listings under Appearance > Menus

    \'publicly_queryable\'  => false, // Will not show up in search results
    \'query_var\'           => false, // Cannot access via ?{query_var}=
    \'rewrite\'             => false, // Entries should not be accessed by URL

    \'has_archive\'         => \'directory_category\', // Use our custom taxonomy
    \'menu_position\'       => 1,  // Should be on top of the parent menu\'s list
    \'supports\'            => array(
          \'page-attributes\', // Used for Sort Order
        )
   );
   register_post_type( \'directory_post\', $args );

  // Create a custom category section for the Directory page
  register_taxonomy(
    \'directory_category\',
    \'directory_post\',
    array(
      \'rewrite\' => false,
      \'hierarchical\' => true,
      \'sort\' => true,
      \'public\' => true,
      \'show_in_nav_menus\'   => false, // Do NOT show these listings under Appearance > Menus
    )
  );

  register_taxonomy_for_object_type( \'directory_category\', \'directory_post\' );
}
add_action(\'init\', \'dir_register_directory\');

2 个回复
最合适的回答,由SO网友:Radley Sustaire 整理而成

I could not find a built-in way to accomplish this solution, but I have created a workaround

我意识到,如果菜单不能一直打开,为什么不自己打开呢?

我创建了以下两个函数,它们很容易理解。需要在通过管理脚本加载的javascript文件中调用它们(请参见函数wp_enqueue_script and the action hook admin_enqueue_scripts).

用法:

jQuery(function() {
  // Deactivate all currently active menus
  wp_deactivate_menus();

  // Activate the parent menu with an id #menu-pages, as well as the submenu item with
  // the specific slug "edit.php?post_type=page"
  wp_activate_menu(\'#menu-pages\', \'edit.php?post_type=page\');
});
功能:
// Deactivate all active / expanded Wordpress menus
function wp_deactivate_menus() {
  var $sidebar = jQuery("#adminmenu");
  var $active_menus = $sidebar.children(\'li.current, li.wp-has-current-submenu, li.wp-menu-open\');

  // Close all open menus
  $active_menus.each(function() {
    var $this = jQuery(this);

    // Conditional classes
    if ($this.hasClass(\'wp-has-current-submenu\')) 
      $this.addClass(\'wp-not-current-submenu\');

    // Unconditional classes
    $this
      .removeClass(\'current\')
      .removeClass(\'wp-menu-open\')
      .removeClass(\'wp-has-current-submenu\')
      .addClass(\'wp-not-current-submenu\');

    // Remove "current" from all submenu items, too
    $this.find(\'ul.wp-submenu li a.current\').removeClass(\'current\');
  });
}

// Activate a Wordpress menu and optionally highlight a submenu slug within that category
// menu_id = String, such as "#my-menu-id". (Not necessarily an ID, but a selector to select the <li>) 
// slug = String, such as "edit.php?post-type=page". Must be exactly the same href as the submenus a[href]
function wp_activate_menu( menu_id, slug ) {
  var $sidebar = jQuery("#adminmenu");
  var $menu = $sidebar.find( menu_id );

  if (!$menu || $menu.length < 1) return false;

  // Conditional classes
  if ($menu.hasClass(\'wp-has-submenu\'))
    $menu.addClass(\'wp-has-current-submenu\');

  // Unconditional classes
  $menu
    .addClass(\'current\')
    .addClass(\'wp-menu-open\')
    .removeClass(\'wp-not-current-submenu\');

  if (typeof slug == \'undefined\') return;

  // Begin activating the submenu
  var $submenu = $menu.find(\'a[href="\' + slug + \'"]\');

  if (!$submenu || $submenu.length < 1) return;

  $submenu.parent(\'li\').addClass(\'current\');
}
这段代码可以激活wordpress菜单。任何菜单。但是,它不会检查should 激活菜单。你自己做吧。我使用了以下if语句:

// getParameterByName function provided by:
// http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values
if (
  getParameterByName(\'taxonomy\') == \'directory_category\'
  && getParameterByName(\'post_type\') == \'directory_post\'
)

SO网友:chaladi

我通过添加以下函数解决了这个问题,

    add_action(\'parent_file\', \'keep_taxonomy_menu_open\');
    function keep_taxonomy_menu_open($parent_file) {
      global $current_screen;
      $taxonomy = $current_screen->taxonomy;
      if ($taxonomy == \'directory_category\')
        $parent_file = \'directory\';
      return $parent_file;
    }
如果要添加更多分类法,只需更改上面代码中的If条件,如下所示

    if ($taxonomy == \'directory_category\' or $taxonomy==\'tax2\' or $taxonomy==\'tax3)

结束

相关推荐

注册_NAV_MENUS,然后以编程方式创建菜单

我有一个register_nav_menus 调用我的主题函数。php://register nav menu and footer nav. register_nav_menus( array( \'main-nav\' => \'Main Navigation\', \'footer-nav\' => \'Footer Navigation\' ) ); 这很好,但我想采取额外的步