M
M
Mixa2015-10-05 18:20:41
PHP
Mixa, 2015-10-05 18:20:41

How to get id of all pages used in wordpress menu?

Suppose there are pages on the site with a description of each employee - each has its own page.
I created a menu and displayed it on all these pages via wp_nav_menu in the site header.
But all these pages are also collected under one roof on the Employees page, where, in addition to the employee's name, which is a link to his page, there should be a brief description, say, of the position.
And so, in order not to type such a page manually and not to duplicate work when new employees appear / dismiss, both in the menu and on the page, I thought it would be cool if such a page was formed automatically from the same menu. Those. on the page with a list of all employees, you just need to display the same menu, only to be formed differently - except for the link and the page name, so that it pulls up some custom fields from the employee's page - the same position, or a photo ...
So here's how you can get an array with IDs of all posts/pages of the specific menu in the same order in which the user configured it? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2015-10-05
@Mixa

/**
         * Получает ID всех  объектов используемых в меню
         *
         * использование  $x = cr_menu_items_ID('primary');
         * пример возвращаемых данных
         * Array ( 
         *      [custom] => Array ( 
         *           [0] => 807 
         *      ) 
         *      [category] => Array ( 
         *          [0] => 2 
         *          [1] => 10 
         *          [2] => 22 
         *      ) 
         *      [page] => Array ( 
         *          [0] => 74 
         *      ) 
         *      [woocommerce] => Array ( 
         *          [0] => 54 
         *          [1] => 39 
         *          [2] => 40
         *      )
         *  )
         *
         * @param $menu_id - идентификатор меню
         * @return array массив с ID элементов меню где ключом будет выступать тип объекта значением массив с id объектов
         */
        function cr_menu_items_ID($menu_id)
        {
            if (($locations = get_nav_menu_locations()) && isset($locations[$menu_id])) {
                $menu = wp_get_nav_menu_object($locations[$menu_id]);
                $menu_items = wp_get_nav_menu_items($menu->term_id);
            }

            foreach ($menu_items as $menu_item) {
               // print_r($menu_item);
                $numbers[$menu_item->object][] = get_post_meta($menu_item->ID, '_menu_item_object_id', true);
            }

            return $numbers;
        }

B
Boris Shepelev, 2015-10-05
@bshepelev

I would advise you to implement using custom data types: register_post_type (link to the code - https://codex.wordpress.org/Function_Reference/reg...
in the future, you can program custom fields there too!
In principle, you can implement it as you want!
but since we will have the following form in the generated menu:
and the number 94 is not the page ID, but the ID of the menu identifier, you will need to cut out the links and look for pages on them! which, in my opinion, is not justified!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question