Come Aggiungere una Pagina Sottomenu a un Tipo di Post Personalizzato?

22 ago 2012, 16:06:28
Visualizzazioni: 58.2K
Voti: 24

Sto cercando di creare un sottomenu sotto un Tipo di Post Personalizzato (Custom Post Type) che ho chiamato Portfolios.

Quando cambio add_submenu_page() in add_options_page(), viene correttamente mostrato un nuovo link sotto il menu Impostazioni, ma non appare sotto il menu Portfolios.

Cosa sto sbagliando?

Di seguito il mio frammento di codice:

add_action( 'admin_menu', 'mt_add_pages' );

function mt_add_pages() {
    add_submenu_page(
        __( 'portfolios', 'menu-test' ), // testo da tradurre
        __( 'Impostazioni Test', 'menu-test' ), // testo da tradurre
        'manage_options',
        'testsettings',
        'mt_settings_page'
    );

    function mt_settings_page() {
        echo "<h2>" . __( 'Impostazioni Test', 'menu-test' ) . "</h2>";
    }
}
1
Commenti

penso che tu stia passando uno slug genitore errato, ad esempio portfolios. controllalo di nuovo... al posto di portfolios passa lo slug del tuo custom post type..

Prince Singh Prince Singh
23 ago 2013 14:38:17
Tutte le risposte alla domanda 4
1
45

add_options_page() aggiunge automaticamente la voce sotto Impostazioni, mentre add_submenu_page() ti dà il controllo su dove vuoi che appaia.

Prova qualcosa del genere:

add_submenu_page(
    'edit.php?post_type=portfolios',
    __( 'Impostazioni di Test', 'menu-test' ),
    __( 'Impostazioni di Test', 'menu-test' ),
    'manage_options',
    'testsettings',
    'mt_settings_page'
);
15 dic 2012 06:22:45
Commenti

Il tuo codice non funzionerà a meno che non includi il terzo argomento menu_title. Consulta il codex

Rahil Wazir Rahil Wazir
2 nov 2013 13:37:41
0

Per ampliare l'esempio di @Jai...

Le mie impostazioni

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

Tipo di post personalizzato

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

register_post_type( 'foo', $args );

Tassonomia personalizzata per le categorie

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

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

Aggiungi le categorie come voci del sottomenu

    $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, ''); 
        }
    } 
20 gen 2018 20:39:45
1
add_submenu_page('edit.php?post_type='.$this->plugin->posttype, __('Impostazioni', $this->plugin->name), __('Impostazioni', $this->plugin->name), 'manage_options', $this->plugin->name, array(&$this, 'adminPanel'));

adminPanel è il nome della funzione di callback.

3 gen 2017 07:11:08
Commenti

Puoi spiegare un po' di più?

bravokeyl bravokeyl
3 gen 2017 08:05:41
0
/**
* Aggiunge una pagina di sottomenu sotto un tipo di post personalizzato come genitore.
*/
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'
    );
}

/**
* Callback per visualizzare la pagina di sottomenu.
*/
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 alla Fonte , Autore: Christina Blust

21 ott 2018 07:38:08