简体   繁体   English

如何在Wordpress中添加活动状态和图标wp_nav_menu()

[英]How to add Active states and icons in wordpress wp_nav_menu()

function main_nav() { 
    wp_nav_menu( 
        array( 
            'menu' => 'main_nav', 
            'theme_location' => 'main_nav', 
            'container_class' => 'menu clearfix', 
            'link_before'     => '<span>',
            'link_after'      => '</span>',
            'fallback_cb' => 'bones_main_nav_fallback' 
        )
    );
}

I'm trying to use link_before and link_after to append a span tag to the wp_nav_menu so so that I may add an icon to each navigation. 我正在尝试使用link_before和link_after将span标签附加到wp_nav_menu,以便为每个导航添加图标。

Example: 例:

<li><span><img src="home.gif" /></span><a href="home.php"> Home</a></li>

I am super super new to php and wordpress. 我是php和wordpress的超级超级新手。 Any ideas on how to tackle this? 关于如何解决这个问题的任何想法?

Secondary question, Adding a css-class to the "current active state" anchor? 第二个问题,是否将CSS类添加到“当前活动状态”锚点? Just for styling. 仅用于样式。

Perhaps you will need to change this code... but here is example. 也许您需要更改此代码...但是这里是示例。 You can see the Reference to wp_nav_menu and add holder pattern... and later just change it by your local replaments settings (see array before replacements) 您可以看到对wp_nav_menu引用,并添加持有人模式...,稍后只需通过本地替换设置进行更改(请参阅替换前的数组)

function main_nav() { 
    $menu = wp_nav_menu( 
        array( 
            'menu' => 'main_nav', 
            'theme_location' => 'main_nav', 
            'container_class' => 'menu clearfix', 
            'link_before'     => '<span></span>',
            'echo'            => $false,
            'fallback_cb' => 'bones_main_nav_fallback' 
        )
    );

    $patterns = array(
        '<span></span><a href="home.php"',
    );
    $replacements = array(  
        '<span><img src="home.gif" /></span><a href="home"'
    );

    echo str_repalce($patterns, $replacements, $menu); 

}

and btw, list items contain number of current classes so you able to use tham to track current item of your menu too. 顺便说一句,列表项包含当前类的数量,因此您也可以使用tham来跟踪菜单的当前项。

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

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