简体   繁体   English

将自定义菜单项添加到WordPress菜单

[英]Adding custom menu item to WordPress menu

I have a WordPress menu that has a few menu items I added through the standard (drag and drop) WordPress admin menu feature. 我有一个WordPress菜单,其中有一些菜单项我通过标准(拖放)WordPress管理菜单功能添加。 Recently I had to add another item to the menu that generates a dynamic href link. 最近我不得不在菜单中添加另一个项目,生成动态href链接。 I achieved that using the following code in my functions.php file: 我在functions.php文件中使用以下代码实现了这一点:

//add my profile menu item dynmacially to the members menu (generate user name based on current user logged in) //将我的个人资料菜单项dynmacially添加到成员菜单(根据当前用户登录生成用户名)

add_filter('wp_nav_menu_items','add_profilelink_in_menu', 10, 2); add_filter('wp_nav_menu_items','add_profilelink_in_menu',10,2);

function add_profilelink_in_menu( $items, $args ) { function add_profilelink_in_menu($ items,$ args){

 if( $args->theme_location == 'secondary') { global $current_user; //converts user id to username $user_info = get_userdata($current_user->ID); $items .='<li id="menu-item-2091" class="menu-item menu-item-2091"> <a href="https://www.mysite.com/members/' . $user_info->user_login .'">Profile</a> </li>'; } return $items; 

} }

My problem is that this menu item is added to the end of the menu and the regular WordPress Menu classes such as 'current-menu-item' don't get applied to this item. 我的问题是这个菜单项被添加到菜单的末尾,并且常规的WordPress菜单类(例如'current-menu-item')不会应用于此项目。 Is there a way for me to control the position of where this menu item is added to (For example: add this item after the first two items?) 有没有办法让我控制添加此菜单项的位置(例如:在前两项之后添加此项?)

and how can I get WordPress to treat this dynamically generated menu item as a regular menu item and have it add all the classes that it adds the other menu items (created through the WordPress menu feature)? 我如何让WordPress将这个动态生成的菜单项视为常规菜单项并让它添加所有添加其他菜单项的类(通过WordPress菜单功能创建)?

Thanks for any help. 谢谢你的帮助。

Here's the logic that you can base using jquery 这是您可以使用jquery建立的逻辑

  //suppose your menu is this
  <ul id="secondary_nav">
    <li id="li_unique_id_1"><a href="">menu 1</a></li>
    <li id="li_unique_id_2"><a href="">menu 2</a></li>
    <li id="li_unique_id_4"><a href="">menu 4</a></li>
</ul>

 //the jquery workaround
 //place this in your footer
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script type='text/javascript'>
$(function(){
    <?php
     global $current_user;            
     //converts user id to username           
     $user_info = get_userdata($current_user->ID);
    ?>
    $("<li id='menu-item-2091' class='menu-item menu-item-209'><a href='https://www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>").insertAfter("#secondary_nav #li_unique_id_2");     
});   
</script>

You can also use insertBefore function 您还可以使用insertBefore函数

Did you check Wordpress menu option in the themes->menu for this ? 您是否在主题 - >菜单中检查了Wordpress菜单选项? You can easily add menu from there also you can set custom menu from there.Hope it will help you. 您可以从那里轻松添加菜单,也可以从那里设置自定义菜单。希望它能为您提供帮助。

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

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