Support » Plugins » Get the parent item using a submenu item (page/category page)

  • Hi,

    My goal is to get the Parent item of a submenu. I want it to be displayed in the content as a Page/Category Title. My menu compose of a mixture of pages and category. below is a sample layout of my blog. Ive got a main menu that has sub menu items. And a sidebar that only contains the submenu of the main menu.

    ----------------------------------------------------------------------------
    |item 1 | item 2 | item 3 | item 4 |                           | Some item |
    ----------------------------------------------------------------------------
                              | item 4 Sub 1 | <-- this is a category page
      display the parent      ----------------
      item title              | item 4 Sub 2 | <-- this is a static page
       here                   ----------------
         V               |  <h3>Page/Category Title</h3>
    <h2>Item 4</h2>      |
                         |       < some page contents >
    item 4 Sub 1         |       < some page contents >
    ------------         |              or
    item 4 Sub 2         |       < some category posts >
                         |       < some category posts >
                         |       < some category posts >
                         |
                         |

    Im currently using this code but it only works if the page/post is a page and not a category page (i got it from this blog: http://pateason.com/wordpress-menu-top-most-parent-id/):

    -------------------------------------------------------------------------
    function get_menu_parent_ID($menu_name){
        if(!isset($menu_name)){
          return "No menu name provided in arguments";
        }
        $menu_slug = $menu_name;
        $locations = get_nav_menu_locations();
        $menu_id   = $locations[$menu_slug];
        $post_id        = get_the_ID();
        $menu_items     = wp_get_nav_menu_items($menu_id);
        $parent_item_id = wp_filter_object_list($menu_items,array('object_id'=>$post_id),'and','menu_item_parent');
        $parent_item_id = array_shift( $parent_item_id );
        function checkForParent($parent_item_id,$menu_items){
          $parent_post_id = wp_filter_object_list( $menu_items, array( 'ID' => $parent_item_id ), 'and', 'object_id' );
          $parent_item_id = wp_filter_object_list($menu_items,array('ID'=>$parent_item_id),'and','menu_item_parent');
          $parent_item_id = array_shift( $parent_item_id );
          if($parent_item_id=="0"){
            $parent_post_id = array_shift($parent_post_id);
            return $parent_post_id;
          }else{
            return checkForParent($parent_item_id,$menu_items);
          }
        }
        if(!empty($parent_item_id)){
          return checkForParent($parent_item_id,$menu_items);
        }else{
          return $post_id;
        }
      }
    
    $parentID = get_menu_parent_ID('main_nav');
    
    ---------------------------------------------------------------------

    How do I go about it? Are there any other way of doing this?

    I really need your help.

    Thanks,

    David

  • The topic ‘Get the parent item using a submenu item (page/category page)’ is closed to new replies.