简体   繁体   English

如何将自定义菜单项添加到现有的Wordpress菜单?

[英]How can I add a custom menu item to an existing Wordpress menu?

I'd like to add a home icon as the first menu item in my Wordpress menu. 我想将主页图标添加为Wordpress菜单中的第一个菜单项。 How can I do it? 我该怎么做? TO let you know, this code didn't work: 让您知道,此代码不起作用:

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
    if (is_single() && $args->theme_location == 'primary') {
        $items .= '<li>Show whatever</li>';
    }
    return $items;
}

I have this in functions.php: 我在functions.php中有这个:

add_theme_support('nav-menus');
if ( function_exists('register_nav_menus')) {
register_nav_menus(
array(
'main' => 'Main Nav'
)
);
}

and this is how I fire the menu: 这就是我触发菜单的方式:

<?php wp_nav_menu( array('menu' => 'main', 'container' => 'nav' )); ?>

You dont need to add filters and hooks for this. 您不需要为此添加过滤器和挂钩。

You can edit your navigation in your theme files, usually it's in the header.php file. 您可以在主题文件中编辑导航,通常在header.php文件中。 Add a new list item with your icon or text and put the link for it. 添加带有图标或文字的新列表项,并放置其链接。 Plain HTML. 纯HTML。

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

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