web-dev-qa-db-ja.com

カスタム投稿タイプにサブメニューページを追加する方法

私はポートフォリオという名前のカスタム投稿タイプの下にサブメニューを作成しようとしています。

add_submenu_page()add_options_page()に変更すると、設定メニューの下に新しいリンクが正しく表示されますが、ポートフォリオメニューの下には表示されません。

何がおかしいのですか?

以下は私のコードスニペットです。

add_action( 'admin_menu', 'mt_add_pages' );

function mt_add_pages() {
    add_submenu_page(
        __( 'portfolios', 'menu-test' ),
        __( 'Test Settings', 'menu-test' ),
        'manage_options',
        'testsettings',
        'mt_settings_page'
    );

    function mt_settings_page() {
        echo "<h2>" . __( 'Test Settings', 'menu-test' ) . "</h2>";
    }
}
14
zoaCode

add_options_page() は設定の下にそれを自動的に追加します、しかし add_submenu_page() はあなたがそれをどこに表示したいかに関してあなたに制御を与えます。

このようなことを試してください:

add_submenu_page(
    'edit.php?post_type=portfolios',
    __( 'Test Settings', 'menu-test' ),
    __( 'Test Settings', 'menu-test' ),
    'manage_options',
    'testsettings',
    'mt_settings_page'
);
26
NightHawk

@Jaiの例を拡張するには...

私の設定

$postType = 'foo';
$categoryType = 'bar';

カスタム投稿タイプ

            $args = array(
                    'labels'             => array('name'=>$postType, ...),
                    'rewrite'            => array('slug' => 'all-'.$postType),
                    'taxonomies'         => array($categoryType)
            );

register_post_type( 'foo', $args );

カスタムカテゴリ分類

            $args = array(
                    'labels'            => array( 'name' => _x( $categoryType, 'taxonomy general name' )),
                    'rewrite'           => array( 'slug' => $categoryType ),
            );

register_taxonomy( $categoryType, array( $postType ), $args );

サブメニュー項目としてカテゴリを追加する

    $wp_term = get_categories( 'taxonomy='.$categoryType.'&type='.$postType ); 
    if ( $wp_term ) {
        foreach ( $wp_term as $term ) {
            // add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug,                                                  callable $function = '' )
            add_submenu_page(    'edit.php?post_type='.$postType,      $term->name,        $term->name,        'manage_options',   'edit.php?post_type='.$postType.'&'.$categoryType.'='.$term->slug, ''); 
        }
    } 
4
Artistan
add_submenu_page('edit.php?post_type='.$this->plugin->posttype, __('Settings', $this->plugin->name), __('Settings', $this->plugin->name), 'manage_options', $this->plugin->name, array(&$this, 'adminPanel'));

管理パネルはコールバック関数名です。

4
Jai
/**
* Adds a submenu page under a custom post type parent.
*/
function books_register_ref_page() {
    add_submenu_page(
        'edit.php?post_type=book',
        __( 'Books Shortcode Reference', 'textdomain' ),
        __( 'Shortcode Reference', 'textdomain' ),
        'manage_options',
        'books-shortcode-ref',
        'books_ref_page_callback'
    );
}

/**
* Display callback for the submenu page.
*/
function books_ref_page_callback() { 
    ?>
    <div class="wrap">
        <h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
        <p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
    </div>
    <?php
}

ソースへのリンク 、作者:Christina Blust

1
Lincoln Lemos