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

Adding menu items dynamically using wp_nav_menu_objects

programmeradmin0浏览0评论

On my primary navigation, I want each top level page to have a list of the subpages. So I created code that dynamically adds menu items to the primary navigation. This works only on the first level of subpages. You can see that I commented out the code that goes down to the second level because it output those links on the main level. I don't know why it does not work on the second level. Could it be that we cannot assign a menu item to a dynamically generated parent menu item?

It looks like:

add_filter( 'wp_nav_menu_objects', 'epc_wp_nav_menu_items', 10, 2 );
function epc_wp_nav_menu_items($items, $args) {
    if ($args->theme_location == 'primary') {
        $menu_order = 10000;
        $menu_order2 = 20000;
        global $wpdb;
        global $post;
        $post_id = 0;       
        if($post){
            $post_id = $post->ID;
        }

        foreach($items as $item){
            $pageChildren = $wpdb->get_results("SELECT post_title, ID FROM wp_posts WHERE post_parent = ".$item->object_id." AND post_status = 'publish' AND post_type='page' ORDER BY menu_order", 'OBJECT' );
            foreach($pageChildren as $child){
                $menu_order++;
                $new_item = epc_add_menu_item($post_id, $menu_order, $item, $child);
                $items[] = (object)$new_item;
                /*
                $pageChildrenSecondLevel = $wpdb->get_results("SELECT post_title, ID FROM wp_posts WHERE post_parent = ".$child->ID." AND post_status = 'publish' AND post_type='page' ORDER BY menu_order", 'OBJECT' );
                foreach($pageChildrenSecondLevel as $child2){
                    $menu_order2++;
                    $new_item2 = epc_add_menu_item($post_id, $menu_order2, $new_item, $child2);
                    $items[] = (object)$new_item2;
                }
                */
            }
        }
    }
    return $items;
}

function epc_add_menu_item($post_id, $menu_order, $item, $child){
                $new_item = new stdClass;
                $new_item->ID = $menu_order;
                $new_item->menu_item_parent = $item->ID;
                $new_item->url = get_permalink( $child->ID );
                $new_item->title = $child->post_title;
                $new_item->menu_order = $menu_order;
                $new_item->object_id = $child->ID;
                $new_item->post_parent = $item->object_id;
                $classes = 'menu-item';
                $classes .= ' menu-item-parent-' . $item->ID;
                $classes .= ' menu-item-type-post_type';
                $classes .= ' menu-item-object-page';
                if($post_id == $child->ID){
                    $classes .= ' current-menu-item';
                    $classes .= ' page-item';
                    $classes .= ' page-item-' . $child->ID ;
                    $classes .= ' current_page_item';
                }   
                $new_item->classes = $classes;
                return $new_item;
}

On my primary navigation, I want each top level page to have a list of the subpages. So I created code that dynamically adds menu items to the primary navigation. This works only on the first level of subpages. You can see that I commented out the code that goes down to the second level because it output those links on the main level. I don't know why it does not work on the second level. Could it be that we cannot assign a menu item to a dynamically generated parent menu item?

It looks like:

add_filter( 'wp_nav_menu_objects', 'epc_wp_nav_menu_items', 10, 2 );
function epc_wp_nav_menu_items($items, $args) {
    if ($args->theme_location == 'primary') {
        $menu_order = 10000;
        $menu_order2 = 20000;
        global $wpdb;
        global $post;
        $post_id = 0;       
        if($post){
            $post_id = $post->ID;
        }

        foreach($items as $item){
            $pageChildren = $wpdb->get_results("SELECT post_title, ID FROM wp_posts WHERE post_parent = ".$item->object_id." AND post_status = 'publish' AND post_type='page' ORDER BY menu_order", 'OBJECT' );
            foreach($pageChildren as $child){
                $menu_order++;
                $new_item = epc_add_menu_item($post_id, $menu_order, $item, $child);
                $items[] = (object)$new_item;
                /*
                $pageChildrenSecondLevel = $wpdb->get_results("SELECT post_title, ID FROM wp_posts WHERE post_parent = ".$child->ID." AND post_status = 'publish' AND post_type='page' ORDER BY menu_order", 'OBJECT' );
                foreach($pageChildrenSecondLevel as $child2){
                    $menu_order2++;
                    $new_item2 = epc_add_menu_item($post_id, $menu_order2, $new_item, $child2);
                    $items[] = (object)$new_item2;
                }
                */
            }
        }
    }
    return $items;
}

function epc_add_menu_item($post_id, $menu_order, $item, $child){
                $new_item = new stdClass;
                $new_item->ID = $menu_order;
                $new_item->menu_item_parent = $item->ID;
                $new_item->url = get_permalink( $child->ID );
                $new_item->title = $child->post_title;
                $new_item->menu_order = $menu_order;
                $new_item->object_id = $child->ID;
                $new_item->post_parent = $item->object_id;
                $classes = 'menu-item';
                $classes .= ' menu-item-parent-' . $item->ID;
                $classes .= ' menu-item-type-post_type';
                $classes .= ' menu-item-object-page';
                if($post_id == $child->ID){
                    $classes .= ' current-menu-item';
                    $classes .= ' page-item';
                    $classes .= ' page-item-' . $child->ID ;
                    $classes .= ' current_page_item';
                }   
                $new_item->classes = $classes;
                return $new_item;
}
Share Improve this question asked Mar 4, 2014 at 16:59 JM01JM01 1211 gold badge1 silver badge4 bronze badges 1
  • 3 What it sounds like you are doing is very complicated for a simple filter. I think that I would strongly consider a custom Walker. See: wordpress.stackexchange/… – s_ha_dum Commented Mar 5, 2014 at 2:01
Add a comment  | 

3 Answers 3

Reset to default 1

You should also specify db_id field for the parent menu item:

function epc_add_menu_item( $post_id, $menu_order, $item, $child ) {
    ...
    $new_item->db_id = $menu_order;
    ...
}

On a side note, menu item that has children should also have menu-item-has-children class.

Use this custom class to display the sub menu:

class Wp_Sublevel_Walker extends Walker_Nav_Menu {

    function start_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class='submenu'>\n";
    }

    function end_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }

}

If your custom menu entries are based on posts, categories or other WP objects you can use wp_setup_nav_menu_item(WP_object) to get a menu item object.

$item=wp_setup_nav_menu_item(get_post());

That object will get most of the information set correctly, but not all information is present. We still need to do set some of the properties on the item. We need to set the parent menu item, if you want to make it at the top level give it the value 0.

$item->menu_item_parent = $parentitem->ID;

In order for an item to have children, you need to assign a db_id, but this NEEDS to be the ID of the WP object, it will not work (better I did not get it to work) with any other value. So we need to set that:

$item->db_id = get_the_ID();

If your current entry will have children you add menu-has-children to the classes variable. Note I also add other (css)classes required for my theme.

$item->classes = array("menu-item", "menu-item-type-post_type", "menu-item-object-page", "menu-item-has-children");

You may be able to create your own custom element by creating the item as I described above and than modifying the $item->title and $item->url but keeping the IDs. Just make sure that the ID is unique in the menu level (I did not check if it needs to be unique in the menu).

(For others like me who found this thread while trying to create a custom menu: The menu order needs to be such that the parent is directly followed by its children.)

发布评论

评论列表(0)

  1. 暂无评论