使用wp_nav_menu_items过滤器添加自定义菜单项


8

我添加了一个片段,以将“个人资料”链接添加到我的网站导航菜单。我的代码:

add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link');
function my_nav_menu_profile_link($menu) {  
    if (!is_user_logged_in()){
         return $menu;
    } else {
         $user_data = bbp_get_user_profile_url( get_current_user_id() );
         $profilelink = '<li><a href="'.$user_data.'&edit" >Profile</a></li>';
         $menu = $menu . $profilelink;
         return $menu;
    }
}

此代码正确显示了我的菜单中的个人资料链接,但是现在我想将此“个人资料”链接移动为另一个主菜单的子菜单。

我的菜单结构如下:

设为首页 我的帐号| 分类目录

我想在“我的帐户”下添加“个人资料”链接。有解决这个问题的建议吗?



@bestprogrammerintheworld-用于管理菜单,而不是前端菜单。
Stephen Harris 2014年

如果您希望添加没有链接的菜单项,请尝试在js的帮助下进行。kvcodes.com/2014/07/…–
Kvvaradha'2

Answers:


6

我已经创建了这两个函数,可用于将自定义项添加到菜单中存在的给定菜单项(页面,帖子,链接...)。

在您的情况下,您可以将以下函数添加到您的functions.php中,并按以下方式调用它们:

$menu_name = 'Your Menu Name';
$name_of_menu_item_to_append_to = 'My Account';
$id_of_menu_item_to_append_to =  get_wp_object_id( $name_of_menu_item_to_append_to, 'nav_menu_item' );
$new_submenu_item = array(
    'text' => 'Profile',
    'url'  => 'http://someurl.com'
);

add_subitems_to_menu( 
    $menu_name,
    $id_of_menu_item_to_append_to,
    array( $new_submenu_item ) 
);

add_subitems_to_menu()

/**
 * Adds custom items to a navigation menu
 * Partially based on: 
 * http://teleogistic.net/2013/02/dynamically-add-items-to-a-wp_nav_menu-list/
 * 
 * @param string    $menu_name          The name or slug of the navigation menu
 * @param int       $parent_object_id   The id of the post/page, which must be present 
 *                                      in the menu, and to which we want to add subitems 
 * @param array     $subitems           The sub-items to be added to the menu, as an
 *                                      array( array( 'text' => 'foo', 'url' => '/bar') )
 */
public function add_subitems_to_menu( $menu_name, $parent_object_id, $subitems ) {
    // Don't add anything in admin area. Otherwise WP will try to display the items in the 
    // Menu editor and it won't work fine and cause strange behaviour
    if ( is_admin() ) {
        return;
    }

    // Use wp_get_nav_menu_items filter, is used by Timber to get WP menu items
    add_filter( 'wp_get_nav_menu_items', function( $items, $menu ) 
            use( $menu_name, $parent_object_id, $subitems ) {

        // If no menu found, just return the items without adding anything
        if ( $menu->name != $menu_name && $menu->slug != $menu_name ) {
            return $items;
        }

        // Find the menu item ID corresponding to the given post/page object ID
        // If no post/page found, the subitems won't have any parent (will be on 1st level)
        $parent_menu_item_id = 0;
        foreach ( $items as $item ) {
            if ( $parent_object_id == $item->object_id ) {
                $parent_menu_item_id = $item->ID;
                break;
            }
        }

        $menu_order = count( $items ) + 1;

        foreach ( $subitems as $subitem ) {
            // Create objects containing all (and only) those properties from WP_Post 
            // used by WP to create a menu item
            $items[] = (object) array(
                'ID'                => $menu_order + 1000000000, // ID that WP won't use
                'title'             => $subitem['text'],
                'url'               => $subitem['url'],
                'menu_item_parent'  => $parent_menu_item_id,
                'menu_order'        => $menu_order,
                // These are not necessary, but PHP warning will be thrown if undefined
                'type'              => '',
                'object'            => '',
                'object_id'         => '',
                'db_id'             => '',
                'classes'           => '',
            );
            $menu_order++;
        }
        return $items;
    }, 10, 2);
}

get_wp_object_id()

 /**
 * Returns the WordPress ID of any post type or page by its title or name
 * In the case you provide an ID it will "validate" it looking for any post with that ID
 *
 * @param mixed     $post_identifier    The title, name or ID of the post/page
 * @param string    $post_type          The post type to look for (default: page)
 *
 * @return int The ID of the post/page if any, or 0
 */
public function get_wp_object_id( $post_identifier, $post_type = 'page' ) {

    $post_id = 0;

    if ( get_page_by_title( $post_identifier, OBJECT, $post_type ) ) {
        $post_id = get_page_by_title( $post_identifier, OBJECT, $post_type )->ID;
    }
    else if ( get_page_by_path( $post_identifier, OBJECT, $post_type ) ) {
        $post_id = get_page_by_path( $post_identifier, OBJECT, $post_type )->ID;
    }
    else if ( get_post( $post_identifier ) ) {
        $post_id = get_post( $post_identifier )->ID;
    }

    return $post_id;
}

谢谢MikO的帮助。1.我的帐户不是页面,我建立了链接,并通过wp管理员菜单设置将其添加到菜单中。2.我通过创建新页面并将其ID传递给函数来检查代码,但是对我不起作用3.我将$ my_account_page_id =行更改为$ my_account_item_id
2014年

我已经添加在function.php所有的代码
Hafsal

哦,Miko,它现在正在工作,非常感谢您提供的代码。.但是,我不想使用页面ID,而是想在我在wp管理菜单设置中创建的链接下添加配置文件
2014年

没有问题,我通过签入db获得了自定义菜单项ID,因此它已修复,再次感谢
Hafsal 2014年

@Hafsal,不客气。我编辑了添加其他功能的答案,您可以使用该功能获取任何WordPress页面,帖子或菜单项的ID,并更新了调用这些功能的方式。否则,您可以直接检查数据库中的ID ...
MikO 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.