将自定义菜单项添加到 WordPress 菜单 [英] Adding custom menu item to WordPress menu

查看:42
本文介绍了将自定义菜单项添加到 WordPress 菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WordPress 菜单,其中包含一些我通过标准(拖放)WordPress 管理菜单功能添加的菜单项.最近我不得不向菜单中添加另一个生成动态 href 链接的项目.我在我的functions.php文件中使用以下代码实现了这一点:

I have a WordPress menu that has a few menu items I added through the standard (drag and drop) WordPress admin menu feature. Recently I had to add another item to the menu that generates a dynamic href link. I achieved that using the following code in my functions.php file:

//将我的个人资料菜单项动态添加到会员菜单(生成用户名基于当前登录的用户)

//add my profile menu item dynmacially to the members menu (generate user name based on current user logged in)

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;  

}

我的问题是此菜单项被添加到菜单的末尾,并且常规的 WordPress 菜单类(例如当前菜单项")不会应用于此项目.有没有办法让我控制这个菜单项添加到的位置(例如:在前两个项目之后添加这个项目?)

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. 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?)

以及如何让 WordPress 将此动态生成的菜单项视为常规菜单项,并让它添加所有类以添加其他菜单项(通过 WordPress 菜单功能创建)?

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)?

感谢您的帮助.

推荐答案

这是您可以使用 jquery 的逻辑

Here's the logic that you can base using 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>

你也可以使用insertBefore函数

这篇关于将自定义菜单项添加到 WordPress 菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆