Come Aggiungere <span> a Ogni Link del Menu con il Testo del Link come Attributo Data?

1 feb 2016, 08:08:35
Visualizzazioni: 19.5K
Voti: 9

Come posso ottenere qualcosa come sotto? Il mio codice è questo:

wp_nav_menu(
     array(
       'theme_location' => 'header_menu',
       'container_id' => 'menu',
       'link_before' => '<span data-hover="link-text-here">',  // Aggiunge span prima del link con attributo data
       'link_after' => '</span>',  // Chiude il tag span dopo il testo del link
     )
  );

Voglio ottenere questo risultato:

<nav class="main-nav">
    <li><a href="#"><span data-hover="Home">Home</span></a></li>
    <li><a href="#"><span data-hover="Proyects">Proyects</span></a></li>
</nav>

Per favore, consigliatemi.

0
Tutte le risposte alla domanda 3
2
14

A partire dalla versione 4.4.0 è stato aggiunto il filtro 'nav_menu_item_args'. Questo permette di impostare gli attributi 'link_before' e 'link_after' per ogni elemento del menu.

add_filter('nav_menu_item_args', function ($args, $item, $depth) {
    if ($args->theme_location == 'header_menu') {
        $title             = apply_filters('the_title', $item->title, $item->ID);
        $args->link_before = '<span data-hover="' . $title . '">';
        $args->link_after  = '</span>';
    }
    return $args;
}, 10, 3);
9 giu 2018 13:26:31
Commenti

Non è corretto... di seguito l'output dopo aver utilizzato il filtro...

<li id="menu-item-44" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-24 current_page_item menu-item-44">
    <a href="http://sandbox.test/about/" aria-current="page">
        <span data-hover="About">About</span>
    </a>
</li>
Daren Zammit Daren Zammit
12 apr 2019 10:07:17

grazie. il tuo codice funziona per me

Younes.D Younes.D
31 lug 2021 11:52:19
4

Soluzione 1: Utilizzo di un walker personalizzato

Ho preso spunto da aggiungere una classe span all'interno del tag anchor di wp_nav_menu e ho apportato alcune modifiche per le tue esigenze.

1. Aggiungi questo codice nel tuo functions.php come prima cosa.

class Nav_Walker_Nav_Menu extends Walker_Nav_Menu{
  function start_el(&$output, $item, $depth, $args){
       global $wp_query;
       $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
       $class_names = $value = '';
       $classes = empty( $item->classes ) ? array() : (array) $item->classes;
       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
       $class_names = ' class="'. esc_attr( $class_names ) . '"';

       $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

       $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
       $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
       $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
       $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

       $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'><span data-hover="'. $item->title .'">';
        $item_output .=$args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
        $item_output .= $description.$args->link_after;
        $item_output .= '</span></a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

2. Aggiungi il codice qui sotto nel tuo header.php dove è installato il tuo wp_nav_menu. Di seguito viene spiegato il codice in modo che installi il nuovo menu personalizzato, in questo caso sarebbe Nav_Walker_Nav_Menu.

<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'walker' => new Nav_Walker_Nav_Menu() ) ); ?>

Spero che questo ti sia d'aiuto!

Soluzione 2: Utilizzando wp_list_pages

Per favore controlla questa pagina. Puoi vedere il loro snippet. Se metti gli span attorno ai tag link puoi usare link_before e link_after

wp_list_pages("link_before=<span data-hover=\"link-text-here\">&link_after=</span>");
1 feb 2016 08:17:14
Commenti

dice

Notice: Tentativo di accedere a una proprietà di un oggetto non esistente in /Applications/MAMP/htdocs/wp.dev/wp-content/themes/eta-theme/functions.php alla riga 98

Mohamed Rihan Mohamed Rihan
1 feb 2016 10:00:06

Quindi, qual è il codice alla riga 98?

AddWeb Solution Pvt Ltd AddWeb Solution Pvt Ltd
1 feb 2016 10:02:46

$item_output = $args->before; $item_output .= '<a'. $attributes .'><span data-hover="'. $item->title .'">'; $item_output .=$args->link_before .apply_filters( 'the_title', $item->title, $item->ID ); $item_output .= $description.$args->link_after; $item_output .= '</span></a>'; $item_output .= $args->after;

Mohamed Rihan Mohamed Rihan
1 feb 2016 10:08:01

sembra ok adesso. è perché non ho aggiunto il menu nel dashboard.. grazie

Mohamed Rihan Mohamed Rihan
1 feb 2016 10:08:54
1

Prova quanto segue:

wp_nav_menu(
 array(
   'theme_location' => 'header_menu',
   'container_id' => 'menu',
   'walker' => new description_walker()
 )
);

Aggiungi questo codice al file functions.php:

class description_walker extends Walker_Nav_Menu{
  function start_el(&$output, $item, $depth, $args){
       global $wp_query;
       $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
       $class_names = $value = '';
       $classes = empty( $item->classes ) ? array() : (array) $item->classes;
       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
       $class_names = ' class="'. esc_attr( $class_names ) . '"';

       $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

       $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
       $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
       $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
      $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

       $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'><span>';
        $item_output .=$args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
        $item_output .= $description.$args->link_after;
        $item_output .= '<span></a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
}

In questo modo potrai facilmente aggiungere un tag span...

1 feb 2016 08:17:28
Commenti

ma voglio l'attributo data all'interno dello span

Mohamed Rihan Mohamed Rihan
1 feb 2016 09:15:02