简体   繁体   English

如何在wp_nav_menu的父容器之后添加div?

[英]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. 您可以使用Walker类在特定类之后添加元素。

Add Something like this in your function.php , 在您的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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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