0

I try to achieve a menu like this (this is the actual menu).

enter image description here

There is a tree of projet coordinated by the wordpress menu that manage route and navigation.

Here is a scheme of the menu and the corresponding route.

PROJECT
 |
 |__ PAINT        (SITE_URL/paint)
 |    |__ post_1  (SITE_URL/paint/post_1)
 |
 |__ VIDEO        (SITE_URL/video)
      |__ post_2  (SITE_URL/video/post_2)
      |__ post_3  (SITE_URL/video/post_2)

First of all, know that post_1, post_2 and post_3 are custom post type generated here by CPT UI.

I need to get this menu in Js and in php, so I created a class in order to parse the menu returned by wp_get_nav_menu_items. You will find the class FP_Nav inside fp_nav.php this git gist.

As the route doesn't really exist while it's set in the wordpress menu as Custom link (ie. /paint, /video in the below scheme). I use an action do_parse_request in order to prevent the native parse_request if the url match some of mine in my menu and let the parse_request execute if not. (find my custom "router" in fp_router.php also in the previous git gist

Here is my functions.php (in the same git gist) in order to know the order of how it's executed.

The problem is:

When I load my class FP_Nav that is supposed to get all the item in the menu using wp_get_nav_menu_items and that should be run before the headers are sent (because I need to parse the request using the menu items), this function wp_get_nav_menu_items does not return any of my custom post that is set in the menu.

I declare my CPT in the action init but I suspect that my class FP_Nav is loaded before the CPT is loaded because in the index.php (template file), the menu got every fields and if I declare the CPT outside the init action as well.

Also I have to load this class BEFORE the headers are sent so I can move out the CPT declaration outstide the init action but I'm not sure it's a good idea and somehow I would like to produce good code so if there is another solution, I'm on!

0

I fixed the problem by putting classes init in a wordpress hook wp_loaded as:

function instantiate_class() {
  FP_Nav();
  FP_Router();
}

add_action( 'wp_loaded', 'instantiate_class', 100 );

which is before the do_parse_request action and after my CPT init in the hook init.

I don't know if it is the best way to handle this but it works well for now.

|improve this answer|||||

Your Answer

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

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