WordPress。在 wp_nav_menu 中手动添加菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12361934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress. Adding menu item manually in wp_nav_menu
提问by user1441797
In my theme there is a function for nav menus
在我的主题中有一个导航菜单功能
function ct_nav() {
<nav>
<?php wp_nav_menu( array( 'container_id' => 'nav', 'theme_location' => 'primary') ); ?>
</nav>
}
How could i add more item manually? using this function alone.
我如何手动添加更多项目?单独使用这个功能。
回答by memical
here is an example by changing the items_wrap.
这是更改 items_wrap 的示例。
wp_nav_menu( array( 'items_wrap' => '<ul id="%1$s" class="%2$s"><li><a href="http://www.google.com">go to google</a></li>%3$s</ul>' ) );
just took the default value and added the href.
只是采用默认值并添加了href。
回答by Said Erraoudy
function add_last_nav_item($items) {
return $items .= '<li><a href="#myModal" role="button" data-toggle="modal">Contact</a></li>';
}
add_filter('wp_nav_menu_items','add_last_nav_item');
回答by Andriy Lesyuk
Just for the case someone needs this:
只是在有人需要这个的情况下:
Menu items can be added manually by applying filters:
可以通过应用过滤器手动添加菜单项:
wp_nav_menu_items
- for all menuswp_nav_menu_{$menu->slug}_items
- for menu with particular slug
wp_nav_menu_items
- 适用于所有菜单wp_nav_menu_{$menu->slug}_items
- 对于带有特定 slug 的菜单
Also by changing items_wrap
, e.g., by removing <ul>
and adding it explicitly in the theme - this way you will be able to add own items.
同样通过更改items_wrap
,例如,通过<ul>
在主题中明确删除和添加它 - 这样您就可以添加自己的项目。
回答by Evan Parsons
None of the above answers worked for me. This is a jquery type of workaround I used. I needed to add an image to the end of my menu.
以上答案都不适合我。这是我使用的一种 jquery 类型的解决方法。我需要在菜单的末尾添加一个图像。
Use wp_nav_menu() as per normal, be sure to specify a class in menu_class or you can specify an ID.
照常使用 wp_nav_menu(),一定要在 menu_class 中指定一个类,或者您可以指定一个 ID。
$items = array(
'theme_location' => 'header-menu',
'menu' => '',
'container' => 'div',
'container_class' => 'menu-{menu slug}-container',
'container_id' => '',
'menu_class' => 'menuContainer', /* important, since we're targetting it with jquery*/
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => ''
);
wp_nav_menu($items);
$( document ).ready(function() {
$(".menuContainer ul").append("<li><img src='<?php echo get_template_directory_uri(); ?>/img/menuImage.png'></li>");
});