我希望在wordpress菜单上有以下输出
Link
Link
Sub Menu Trigger
Sub Menu Link
Sub Menu Link
然而,我不知道如何使用Nav (我仍在用我的头)。是否有可能:
我不确定这是否可能。如果不是的话,还有别的办法可以帮我吗?或者有可能,但很复杂?
发布于 2019-02-21 21:05:52
是的,有可能.你只需要写你自己的沃克课。这里有一个起点:
class My_Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
public function start_lvl( &$output, $depth = 0, $args = array() ) {
if ( $depth ) {
$output .= 'Sub Menu Trigger';
}
}
public function end_lvl( &$output, $depth = 0, $args = array() ) {
if ( $depth ) {
$output .= '';
}
}
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
$title = apply_filters( 'the_title', $item->title, $item->ID );
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$item_output = $args->before;
$item_output .= '';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
// don't do anything - elements have no endings
}
}
https://wordpress.stackexchange.com/questions/329459
复制相似问题