Добавить пользовательский пункт меню, используя фильтр 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 - это меню администратора, а не интерфейсное меню.
Стивен Харрис

если вы хотите добавить пункт меню без ссылки, попробуйте этот с помощью js. kvcodes.com/2014/07/…
Квварадха

Ответы:


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. Я проверил ваш код, создав новую страницу и передав ее идентификатор функции, но не работает для меня 3. Я изменил строку $ my_account_page_id = на $ my_account_item_id
Hafsal

Я добавил весь ваш код в function.php
Hafsal

Ох, Мико, он работает сейчас, большое спасибо за код .. Но вместо того, чтобы использовать идентификатор страницы, я хотел бы добавить профиль по ссылке, которую я создал в настройках меню администратора wp
Hafsal

Никаких проб, я установил идентификатор пользовательского пункта меню, установив в db Так что это исправлено, еще раз спасибо
Hafsal

@ Хафсал, пожалуйста. Я отредактировал свой ответ, добавив другую функцию, которую вы можете использовать для получения идентификатора любой страницы WordPress, поста или пункта меню, и обновил способ вызова этих функций. В противном случае вы можете проверить непосредственно идентификатор в БД, как вы сделали ...
MikO
Используя наш сайт, вы подтверждаете, что прочитали и поняли нашу Политику в отношении файлов cookie и Политику конфиденциальности.
Licensed under cc by-sa 3.0 with attribution required.