最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

menus - wp_get_nav_menu_items doesn't return custom post type item

programmeradmin1浏览0评论

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

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!

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

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!

Share Improve this question asked Oct 24, 2019 at 8:10 Liroo PierreLiroo Pierre 1012 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论