0

Basically I'm customizing the menu through the options of wp_nav_menu().

Here is some of my code:

function custom_novice_menu($args) {
    $args['container'] = false;
        $args['container_id'] = 'my_primary_menu';
        $args['link_before'] = '<div class="topic-card"><div class="topic-circle"></div><span></span><h3>';
        $args['link_after'] = '</h3></div>';
    return $args;
}

Essentially I want to get the name or ID of the current menu item. That way I can use custom fields for each menu item, but I'm not sure how to get the current item doing this.

This is a filter in the functions.php file.

I basically want $item->name or something like that I'm assuming.

1

The object_id holds the ID of whatever object the menu item refers to, this will be a post, post etc. The get_queried_object_id() will give you the ID of the current page.

$menu_items = wp_get_nav_menu_items( 'main-menu' );
$this_item = current( wp_filter_object_list( $menu_items, array( 
'object_id' => get_queried_object_id() ) ) );
echo $this_item->title;

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.