简体   繁体   中英

How to add div after parent container for wp_nav_menu?

function add_custom_nav( $items, $args ) {
if( $args->theme_location == 'menu-1' ) {
    return  "<div>I AM going CraZY</div>" . $items;
  }
  return $items;
}
add_filter('wp_nav_menu_items','add_custom_nav', 10 , 2);

I looked up a lot of examples, but when I try calling it, there's no change.

<?php wp_nav_menu( array( 'theme_location' => 'menu-1' ) ); ?>

You can use Walker class to add elements after specific class.

Add Something like this in your function.php ,

class Walker_Nav_Pointers extends Walker_Nav_Menu
{
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    $output .= sprintf( "\n<li><a href='%s'%s>%s</a></li>\n",
        $item->url,
        ( $item->object_id === get_the_ID() ) ? ' class="current"' : '',
        $item->title
    // Add your code to add your elements.
    );
}
}

then,

<?php
wp_nav_menu(array('walker' => new Walker_Nav_Pointers()););
?>

Check this . Hope this will help you. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM