簡體   English   中英

WordPress 在自定義管理菜單下顯示分類

[英]WordPress show taxonomy under custom admin menu

我試圖在管理菜單項下顯示自定義分類法,該菜單項只是一個頁面,即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 );
}

我找到了解決這個問題的方法,代碼如下:

    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;
    }

在您的分類法注冊中:

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 並為其他帖子類型添加相同的內容。

假設我們的分類 slug 將是vendor ,而我們想要將它放在下面的父菜單將是my_menu

要授予訪問權限,將使用默認的manage_categories 功能 這需要匹配在$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;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM