1

I'm working on a filter for nav menu items, such as

add_filter('wp_get_nav_menu_items','nav_items', 11, 3);
function nav_items($items, $menu, $args) {
    foreach ($items as $item) {
        $slug_1 = get_post_field('post_name', $item->ID);
        $slug_2 = $item->post_name;
        // ...
    }
    return $items;
}

But neither $slug_1 and $slug_2 returns the correct slug.

What is the correct way to get the slug associated to a navbar item?

  • Note that the menu item is not the post being linked to, but a post of type nav_menu_item, it's a hidden post type used just for nav menus – Tom J Nowell Oct 2 '18 at 13:58
2

May be duplicate, but I wasn't able to find the answer. $item->object_id stores the actual post ID:

$slug = get_post_field('post_name', $item->object_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.