【问题标题】:WordPress custom taxonomy link not highlighted under custom admin menuWordPress 自定义分类链接未在自定义管理菜单下突出显示
【发布时间】:2013-11-16 23:59:32
【问题描述】:

我已经为我的自定义帖子类型注册了自定义分类:

$labels = array(
    ...labels here...
);

$args = array(
    'label'             => __('Categories', $this->text_domain),
    'labels'            => $labels,
    'public'            => true,
    'show_ui'           => true,
    'show_in_nav_menus' => true,
    'show_admin_column' => true,
    'hierarchical'      => true,
    'query_var'         => true,
    'rewrite'           => array('slug' => 'virtual-product-category'),
);

register_taxonomy('virtual_product_cat', array('virtual_product'), $args);

按预期工作 - 我可以为我的自定义帖子选择一个自定义类别。

然后我将它添加到自定义菜单中:

add_submenu_page(
    'virtual',
    __('Virtual Product Categories', $this->text_domain),
    __('Categories', $this->text_domain),
    'edit_products',
    'edit-tags.php?post_type=virtual_product&taxonomy=virtual_product_cat'
);

它出现了:

当我单击它(“类别”链接)时,分类编辑页面加载正常,但是,父菜单显示为折叠状态,子菜单(“类别”)未突出显示:

另一方面,自定义帖子类型(链接“虚拟产品”)按预期工作(见第一张图片)。

我可以做一些 hacks/workarounds,使用 JS/CSS 使其突出显示,但我认为我在这里遗漏了一些东西..

那么,如何让自定义菜单下的自定义分类菜单链接正常工作呢?

谢谢!

【问题讨论】:

  • 我知道这听起来无关紧要 - 但是 text_domain 翻译是否有效??

标签: wordpress custom-taxonomy


【解决方案1】:

好的,对于所有遇到相同问题的人..

你正在做的是:

  1. 使用add_menu_page 创建自定义菜单
  2. 创建自定义帖子类型/分类
  3. 尝试在新菜单下推送自定义分类编辑链接

改为这样做:

  1. 不要手动创建任何菜单 (!)
  2. 添加自定义帖子类型时,不要设置'show_in_menu' 参数 - 这将为您创建一个菜单。您可以通过在同一 args 数组下设置 'menu_position' 来控制它的显示位置。
  3. 将此新创建的菜单用于您要添加的所有其他自定义页面。调用 add_submenu_page 时,使用 'edit.php?post_type=%%post_type_name%%' 作为父 slug(第一个参数)。

简单地说——不要反其道而行之:)

感谢试图提供帮助的 Obmerk Kronenstink。欣赏这一点。

【讨论】:

  • 能否提供您正在使用的完整代码?因为根据文档,menu_position 不在 register_taxonomy 参数中!
  • @numediaweb 在下面看到我的答案:)
【解决方案2】:

我也在寻找同样的东西,但我不想使用自定义帖子类型作为主菜单。

我最终在 WP.org 支持论坛http://wordpress.org/support/topic/moving-taxonomy-ui-to-another-main-menu找到了这个链接

代码如下。

function recipe_tax_menu_correction($parent_file) {
    global $current_screen;
    $taxonomy = $current_screen->taxonomy;
    if ($taxonomy == 'ingredient' || $taxonomy == 'cuisine' || $taxonomy == 'course' || $taxonomy == 'skill_level')
        $parent_file = 'recipe_box_options';
    return $parent_file;
}
add_action('parent_file', 'recipe_tax_menu_correction');

【讨论】:

    【解决方案3】:

    您可以将其用作模板,也可以像我一样使用taxonomy generator

    // Register Custom Taxonomy
    function Stack_Overflow()  {
    
    $labels = array(
        'name'                       => _x( 'Question', 'Taxonomy General Name', 'stack_overflow' ),
        'singular_name'              => _x( 'Question', 'Taxonomy Singular Name', 'stack_overflow' ),
        'menu_name'                  => __( 'Stack Overflow', 'stack_overflow' ),
        'all_items'                  => __( 'All Questions', 'stack_overflow' ),
        'parent_item'                => __( 'Language', 'stack_overflow' ),
        'parent_item_colon'          => __( 'Framework', 'stack_overflow' ),
        'new_item_name'              => __( 'New Question', 'stack_overflow' ),
        'add_new_item'               => __( '', 'stack_overflow' ),
        'edit_item'                  => __( 'Edit Question', 'stack_overflow' ),
        'update_item'                => __( 'Edit Question', 'stack_overflow' ),
        'separate_items_with_commas' => __( '', 'stack_overflow' ),
        'search_items'               => __( 'Search Questions', 'stack_overflow' ),
        'add_or_remove_items'        => __( 'Add or Remove Questions', 'stack_overflow' ),
        'choose_from_most_used'      => __( 'Choose from the most popular languages', 'stack_overflow' ),
    );
    $rewrite = array(
        'slug'                       => 'questions',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'query_var'                  => 'question',
        'rewrite'                    => $rewrite,
        'update_count_callback'      => 'display_edit_count',
    );
    register_taxonomy( 'stakoverflow', 'page', $args );
    
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'Stack Overflow', 0 );
    

    【讨论】:

      【解决方案4】:

      @sPaul 您在答案中引用的内容适用于自定义帖子类型,但不适用于分类法。自定义分类法没有 'menu_position' 参数。您在问题中所做的工作可以很好地在您的菜单下创建链接:

      add_submenu_page(
          'virtual',
          __('Virtual Product Categories', $this->text_domain),
          __('Categories', $this->text_domain),
          'edit_products',
          'edit-tags.php?post_type=virtual_product&taxonomy=virtual_product_cat'
      );
      

      但要让它显示正确的样式,您需要添加一些 JS 来添加/删除类。方法如下(修改自https://wordpress.stackexchange.com/a/225228/80088):

      add_action( 'admin_head-edit-tags.php', 'modify_menu_highlight_wpse_43839' );
      
      function modify_menu_highlight_wpse_43839()
      {
          if( 'virtual_product_cat' == $_GET['taxonomy'] )
          {       
              ?>
              <script type="text/javascript">
                  jQuery(document).ready( function($) 
                  {
                      $("#menu-posts, #menu-posts a")
                          .removeClass('wp-has-current-submenu')
                          .removeClass('wp-menu-open')
                          .addClass('wp-not-current-submenu'); 
                      $("#toplevel_page_ virtual, #toplevel_page_ virtual > a")
                          .addClass('wp-has-current-submenu');
                  });     
              </script>
              <?php
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-10-07
        • 2016-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多