1

I have a custom menu made up of pages and categories and I am trying to output the category and subcategories as well as the last 4 posts from each category/ subcategory.

In trying to use a walker class I borrowed from an example class

    function walk( $elements, $max_depth) {

    $args = array_slice(func_get_args(), 2);
    $output = '';

    if ($max_depth < -1) //invalid parameter
        return $output;

    if (empty($elements)) //nothing to walk
        return $output;

    $id_field = $this->db_fields['id'];
    $parent_field = $this->db_fields['parent'];

    // flat display
    if ( -1 == $max_depth ) {
        $empty_array = array();
        foreach ( $elements as $e )
            $this->display_element( $e, $empty_array, 1, 0, $args, $output );
        return $output;
    }

    /*
     * need to display in hierarchical order
     * separate elements into two buckets: top level and children elements
     * children_elements is two dimensional array, eg.
     * children_elements[10][] contains all sub-elements whose parent is 10.
     */
    $top_level_elements = array();
    $children_elements  = array();
    foreach ( $elements as $e) {
        if ( 0 == $e->$parent_field )
        {
            $top_level_elements[] = $e;
            $child_posts = array();
            if($e->object == 'category')
            {
              $child_cats = blankslate_get_menu_categories($e->object_id); // custom function to get categories of category
              $child_posts = blankslate_get_menu_posts($e->object_id, 'post', 4, true); // custom function to get posts of category
              if(!empty($child_cats))
              {
                $child_posts = array_merge($child_cats, $child_posts);
                $output = $this->walk($child_cats, 0);
              }
            } else $child_posts = blankslate_get_menu_posts($e->object_id, $e->object);

                if(!empty($child_posts)) $e->classes[] = "menu-item-has-children";
            foreach ( $child_posts as $child ) {
                $child = wp_setup_nav_menu_item($child);
                $child->post_type = 'nav_menu_item';
                $child->menu_item_parent = $e->$id_field;
                $child->ID = $e->$id_field.$child->ID;
                $children_elements[ $e->$id_field ][] = $child;
                $children_elements_classes[] = $child;
            }
        }
        else
        {
            $children_elements[ $e->$parent_field ][] = $e;
        }
    }

    /*
     * when none of the elements is top level
     * assume the first one must be root of the sub elements
     */
    if ( empty($top_level_elements) ) {

        $first = array_slice( $elements, 0, 1 );
        $root = $first[0];

        $top_level_elements = array();
        $children_elements  = array();
        foreach ( $elements as $e) {
          if ( $root->$parent_field == $e->$parent_field )
          {
              $top_level_elements[] = $e;
              if($e->object == 'category')
               {
                 $child_cats = blankslate_get_menu_categories($e->object_id);
                 $child_posts = blankslate_get_menu_posts($e->object_id, 'post', 4, true);
                 if(!empty($child_cats))
                 {
                   $child_posts = array_merge($child_cats, $child_posts);
                 }


               } else $child_posts = blankslate_get_menu_posts($e->object_id, $e->object);
              if(!empty($child_posts)) $e->classes[] = 'menu-item-has-children';
              foreach ( $child_posts as $child ) {
                  $child = wp_setup_nav_menu_item($child);
                  $child->post_type = 'nav_menu_item';
                  $child->menu_item_parent = $e->$id_field;
                  $child->ID = $e->$id_field.$child->ID;
                  $children_elements[ $e->$id_field ][] = $child;
                  $children_elements_classes[] = $child;
              }
          }
          else
          {
              $children_elements[ $e->$parent_field ][] = $e;
          }
        }
    }

    //assing the classes to our dynamically populated posts
    if ( isset($children_elements_classes) )
        _wp_menu_item_classes_by_context($children_elements_classes);

    foreach ( $top_level_elements as $e )
        $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );

    /*
     * if we are displaying all levels, and remaining children_elements is not empty,
     * then we got orphans, which should be displayed regardless
     */
    if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
        $empty_array = array();
        foreach ( $children_elements as $orphans )
            foreach( $orphans as $op )
                $this->display_element( $op, $empty_array, 1, 0, $args, $output );
     }

     return $output;
}

However I'm not sure how to transverse through the fetched categories to get the sub categories and posts in depth. Can anyone advise me on what steps I can take for this walk class to work?

Your Answer

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

Browse other questions tagged or ask your own question.