Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am building a hierarchical menu of custom taxonomies and their related posts using wp_get_nav_menu_items as follow:

if( $locations = get_nav_menu_locations() && isset($locations['secondary']) ):
  $menu = wp_get_nav_menu_object( $locations['secondary'] );

  $menu_items = wp_get_nav_menu_items( $menu->term_id );

  foreach( (array) $menu_items as $key => $menu_item ):
    # access to $menu_item -> title;
    # access to $menu_item -> ID;
    # access to $menu_item -> object_id;
    # etc ...
  endforeach;
endif;
etc ...

Say I have a menu item called service (it is a term of custom taxonomy solutions_category), and from the Appearance -> Menus I changed its label to 'Our Services' through the Navigation Label. In this case $menu_item->title returns 'Our Services'. How can I get 'service' instead? Because I need it for a subsequent WP_Query to get all posts that belong to custom category 'service'

Thanks

P.S. I can post full code if you need.

share|improve this question
    
Have you tried doing a var_dump() on one of your $menu_item's? I suspect that somewhere in that object will be what you're after. There's a lot of info in the Return Values –  MartinB Mar 20 at 17:04

1 Answer 1

up vote 0 down vote accepted

You can try to get that information by using the get_term_by() function with the relevant parameters of the menu item (namely object and object_id) :

foreach( (array) $menu_items as $key => $menu_item )
{
    // Match the menu items to your custom taxonomy:
    if( 'solutions_category' === $menu_item->object )
    {
        // Fetch the corresponding term object:
        $term = get_term_by( 'term_id', $menu_item->object_id, $menu_item->object );

        // Show the term name:
        echo $term->name;

        // Debug:
        // print_r( $term );
    }
}

Note that if you don't want to target a special taxonomy, you can replace

    // Match your custom taxonomy:
    if( 'solutions_category' === $menu_item->object )

with

    // Match only menu items of the type "taxonomy":
    if( 'taxonomy' === $menu_item->type )

where $menu_item->type can have values like taxonomy, post_type, custom (Links), ...

The value of $menu_item->type_label gives you also further information on the menu item type.

Hope this help.

share|improve this answer
    
This is the right solution. Thanks birgire –  blaise Mar 20 at 21:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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