【问题标题】:WordPress show taxonomy under custom admin menuWordPress 在自定义管理菜单下显示分类
【发布时间】:2016-01-04 05:57:41
【问题描述】:

我正在尝试在管理菜单项下显示自定义分类,该菜单项只是一个页面,即http://example.com/wp-admin/admin.php?page=bla

根据 WordPress 开发人员。 show_in_menuregister_taxonomy 下的页面显示以下内容:

'some string' - 如果是现有的顶级页面,例如 'tools.php' 或 'edit.php?post_type=page',则帖子类型将作为其子菜单放置。

这是否意味着分类不能显示在那些下面?

PHP

<?php
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );

// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Genres', 'taxonomy general name' ),
        'singular_name'     => _x( 'Genre', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Genres' ),
        'all_items'         => __( 'All Genres' ),
        'parent_item'       => __( 'Parent Genre' ),
        'parent_item_colon' => __( 'Parent Genre:' ),
        'edit_item'         => __( 'Edit Genre' ),
        'update_item'       => __( 'Update Genre' ),
        'add_new_item'      => __( 'Add New Genre' ),
        'new_item_name'     => __( 'New Genre Name' ),
        'menu_name'         => __( 'Genre' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_in_menu'      => 'bla', // not working | tried admin.php?page=bla as well, also not working
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'genre' ),
    );

    register_taxonomy( 'genre', array( 'book' ), $args );
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我找到了解决这个问题的方法,代码如下:

        add_action( 'admin_menu', 'shmeh_menu' );
        add_action( 'parent_file', 'menu_highlight' );
    
        function shmeh_menu() {
            add_submenu_page( 'bla', 'Shmeh', 'Shmeh', 'manage_options', 'edit-tags.php?taxonomy=shmeh');
        }
    
        function menu_highlight( $parent_file ) {
            global $current_screen;
    
            $taxonomy = $current_screen->taxonomy;
            if ( $taxonomy == 'shmeh' ) {
                $parent_file = 'bla';
            }
    
            return $parent_file;
        }
    

    【讨论】:

    • 谢谢,在网上搜索完
    • 只能部分工作 - 它打开了正确的子菜单,但子菜单页面没有以白色和粗体突出显示
    【解决方案2】:

    在您的分类注册中:

    show_in_menu => true
    

    然后你需要添加一个子菜单页面:

    $parent_slug = 'slug_for_parent_menu';
    $page_title = 'Submenu Page Title';
    $menu_title = 'Submenu Page Title';
    $capability = 'manage_options';
    $menu_slug = 'edit-tags.php?taxonomy=bla&post_type=custom_post_type_name';
    $function = null;
    $position = null;
    add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function, $position);
    

    终于:

    add_filter('parent_file', 'menu_highlight');    
    function menu_highlight($parent_file) {
        global $plugin_page, $submenu_file, $post_type, $taxonomy;
        if ('custom_post_type_name' == $post_type) {
            if ($taxonomy == 'bla') {
                $plugin_page = 'edit-tags.php?taxonomy=bla&post_type= custom_post_type_name'; // the submenu slug 
                $submenu_file = 'edit-tags.php?taxonomy=bla&post_type= custom_post_type_name';    // the submenu slug
            }
        } 
        return $parent_file;
    }
    

    希望我正确地输入了所有内容。将任何其他 if 嵌套在分类检查中,并为其他帖子类型添加相同的内容。

    【讨论】:

      【解决方案3】:

      假设我们的分类 slug 是 vendor,而我们想要将它放在下面的父菜单是 my_menu

      要授予访问权限,使用默认的manage_categories capability。这需要与在$args 参数中传递的capabilities 数组中提供给register_taxonomy 函数的函数相匹配。

      add_action('admin_menu', function (): void {
          add_submenu_page(
              'my_menu',              // parent_slug
              'Vendors',              // page_title
              'Vendors',              // menu_title
              'manage_categories',    // capability
              'edit-tags.php?taxonomy=vendor' // menu_slug
          );
      });
      
      add_action('parent_file', function (string $parent_file): string {
          global $current_screen;
      
          if ($current_screen->taxonomy === 'vendors') {
              return 'my_menu';
          }
      
          return $parent_file;
      });
      

      【讨论】:

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