最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Add a Sub Menu Page to a Custom Post Type?

programmeradmin1浏览0评论

I am trying to create a sub-menu under a Custom Post Type I have named Portfolios.

When I change add_submenu_page() to add_options_page(), it correctly shows a new link under the Settings menu, but it doesn't show under the Portfolios menu.

What am I doing wrong?

Below is my code snippet;

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

I am trying to create a sub-menu under a Custom Post Type I have named Portfolios.

When I change add_submenu_page() to add_options_page(), it correctly shows a new link under the Settings menu, but it doesn't show under the Portfolios menu.

What am I doing wrong?

Below is my code snippet;

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>";
    }
}
Share Improve this question edited Oct 4, 2018 at 0:00 nyedidikeke 4921 gold badge6 silver badges15 bronze badges asked Aug 22, 2012 at 13:06 zoaCodezoaCode 3431 gold badge2 silver badges7 bronze badges 1
  • i think you are passing incorrent parent slug, i.e portfolios. check it again... inplace of portfolios pass the slug of your custom post type.. – Prince Singh Commented Aug 23, 2013 at 11:38
Add a comment  | 

4 Answers 4

Reset to default 45

add_options_page() automatically adds it underneath settings, however add_submenu_page() gives you control as to where you want it to show up.

Try something like this:

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

To expand on @Jai example...

My Settings

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

Custom Post Type

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

register_post_type( 'foo', $args );

Custom Category Taxonomy

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

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

Add Categories as submenu items

    $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, ''); 
        }
    } 
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'));

there is admin panel is a callback function name.

/**
* 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
}

Link to Source , Author: Christina Blust

发布评论

评论列表(0)

  1. 暂无评论