• Christina

    (@carasmo)


    I love this plugin! I have a variety of child menu items and they are not necessarily child pages. This plugin with a widget allows me to show just the child menu items of the current parent, and if there are no children, it won’t output — it’s perfect.

    I just donated in the hopes that the developer will update the plugin so it is fresh and more people use it.

    Here’s my settings:

    I am getting the current parent title by using the following php:

    //* Get the menu parent id
    //* https://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";
        endif;
        
        $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 check_for_parent( $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 check_for_parent( $parent_item_id, $menu_items );
         
          endif;
          
        } //check_for_parent();
        
        if( !empty( $parent_item_id ) ) :
          return check_for_parent($parent_item_id,$menu_items);
        else:
          return $post_id;
        endif;
    }
    
    //get Menu label by ID post or parentID from get_menu_parent_ID();
    //https://stackoverflow.com/a/26591562/1004312
    function get_menu_parent_by_id( $post_id, $menu ) {
    
        $menu_title = '';
        $nav = wp_get_nav_menu_items( $menu );
    
        foreach ( $nav as $item ) :
    
            if ( $post_id == $item->object_id ) :
                $menu_title = $item->post_title;
                break;
            endif;
    
        endforeach;
    
        return ( $menu_title !== '' ) ? $menu_title : get_the_title( $post_id );
    
    }
    
    //* allow shortcode in widget title
    add_filter( 'widget_title', 'do_shortcode' );
    
    //* menu parent title shortcode
    function child_theme_parent_menu_title() {
     
     	$menu_location  = 'primary';
     	$menu_parent_id = get_menu_parent_id( $menu_location );
    	return get_menu_parent_by_id( $menu_parent_id, $menu_location );
    
    } 
    add_shortcode( 'parent-menu-title', 'child_theme_parent_menu_title' );
    
    
    • This topic was modified 7 years ago by Christina.
  • The topic ‘Saves a ton of time and works great in 4.9 too’ is closed to new replies.