X

Come usare l'output del sottomenu separato di WP_nav_menu

Snippet di IsItWP

Stai cercando un modo per utilizzare l'output del sottomenu separato di wp_nav_menu? Sebbene esista probabilmente un plugin per questo, abbiamo creato un rapido snippet di codice che puoi utilizzare per l'output del sottomenu separato di wp_nav_menu in WordPress.

Istruzioni:

Tutto quello che devi fare è aggiungere questo codice al file functions.php del tuo tema o in un plugin specifico per il sito:

/**
 * WP_nav_menu separate submenu output.
 *
 * Optional $args contents:
 *
 * string theme_location - The menu that is desired.  Accepts (matching in order) id, slug, name. Defaults to blank.
 * string xpath - Optional. xPath syntax.
 * string before - Optional. Text before the menu tree.
 * string after - Optional. Text after the menu tree.
 * bool echo - Optional, default is TRUE. Whether to echo the menu or return it.
 *
 * @param array $args Arguments
 * @return String If $echo value is set to FALSE.
 */
function px_the_submenu( $args = array() )
{
 
    $defaults = array(
        'theme_location' => '',
        'xpath' => "./li[contains(@class,'current-menu-item') or contains(@class,'current-menu-ancestor')]/ul",
        'before' => '',
        'after' => '',
        'echo' => true
    );
    $args = wp_parse_args( $args, $defaults );
    $args = (object) $args;
 
    $output = array();
 
    $menu_tree = wp_nav_menu( array( 'theme_location' => $args->theme_location, 'container' => '', 'echo' => false ) );
    $menu_tree_XML = new SimpleXMLElement( $menu_tree );
    $path = $menu_tree_XML->xpath( $args->xpath );
 
    $output[] = $args->before;
 
    if( ! empty( $path ) )
    {
        $output[] = $path[0]->asXML();
    }
 
    $output[] = $args->after;
 
    if( $args->echo )
        echo implode('', $output );
    else
        return implode('', $output );
 
}

Nota: Se questa è la prima volta che aggiungi snippet di codice in WordPress, consulta la nostra guida su come aggiungere correttamente snippet di codice in WordPress, in modo da non compromettere accidentalmente il tuo sito.

Se ti è piaciuto questo snippet di codice, prendi in considerazione la lettura dei nostri altri articoli sul sito come: I 9 migliori plugin per la gestione degli annunci di WordPress per aumentare le entrate e come creare correttamente una pagina di accesso personalizzata in WordPress.

Commenti   Lascia una risposta

Aggiungi un commento

Siamo lieti che tu abbia scelto di lasciare un commento. Tieni presente che tutti i commenti sono moderati secondo la nostra normativa sulla privacy e tutti i link sono nofollow. NON utilizzare parole chiave nel campo del nome. Avviamo una conversazione personale e significativa.

Checklist per il lancio di WordPress

La Guida Definitiva per il Lancio di WordPress

Abbiamo raccolto tutti gli elementi essenziali della checklist per il lancio del tuo prossimo sito web WordPress in un comodo ebook.
Sì, Inviami l'eBook Gratuito!