Log1x / navi

A developer-friendly alternative to the WordPress NavWalker.
https://github.com/Log1x/navi
MIT License
315 stars 30 forks source link

No way to get object ID's #7

Closed codepuncher closed 4 years ago

codepuncher commented 4 years ago

There is currently no way to get the Post ID for the related nav item. This prevents getting a field from a page in a menu.

Updating the parse method allows for this.

      protected function parse($items)
      {
          if (! is_array($items)) {
              return;
          }

          _wp_menu_item_classes_by_context($items);

          return $this->tree(
              collect($items)
                  ->map(function ($item) {
                      return (object) [
                          'parent' => $this->hasParent($item),
                          'id' => $item->ID,
+                         'db_id' => $item->db_id,
+                         'object_id' => $item->object_id,
                          'label' => $item->title,
                          'slug' => $item->post_name,
                          'url' => $item->url,
                          'active' => $item->current,
                          'activeAncestor' => $item->current_item_ancestor,
                          'activeParent' => $item->current_item_parent,
                          'classes' => $this->filterClasses($item->classes),
                          'title' => $item->attr_title,
                          'description' => $item->description,
                          'target' => $item->target,
                          'xfn' => $item->xfn,
                      ];
                  })
          );
      }
Log1x commented 4 years ago

Thanks! If you had a chance, would you mind adding it to the readme example?

codepuncher commented 4 years ago

Sure, I'll submit a PR now!