How to Auto Add Subpages to Menus in WordPress

Home » Snippets » How to Auto Add Subpages to Menus in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

WordPress allows you to easily create menus and add pages to them, but what if you have a lot of subpages that you need to add to your menus? Doing it manually can be time-consuming and tedious. Fortunately, there is a way to automatically add subpages to your menus using a simple function. In this article, we’ll show you how to do it and save yourself time and hassle in the future.

					function wpturbo_auto_add_subpages_to_menus( $menu_items, $args ) {
    if ( 'main-menu' !== $args->menu_slug ) { // Replace 'main-menu' with your menu slug.
        return $menu_items;
    }

    $new_items = array();
    $menu_count = count( $menu_items );

    foreach ( $menu_items as $key => $menu_item ) {
        $new_items[] = $menu_item;
        if ( 'page' === $menu_item->object ) {
            $subpages = get_pages( array(
                'child_of' => $menu_item->object_id,
                'orderby' => 'menu_order',
                'order' => 'ASC',
            ) );

            foreach ( $subpages as $subpage ) {
                $menu_count++;

                $new_items[] = (object) array(
                    'ID' => $menu_count,
                    'menu_item_parent' => $menu_item->ID,
                    'title' => $subpage->post_title,
                    'url' => get_permalink( $subpage->ID ),
                    'menu_order' => $menu_count,
                    'object' => $subpage->post_type,
                    'object_id' => $subpage->ID,
                    'type' => 'post_type',
                );
            }
        }
    }

    return $new_items;
}
add_filter( 'wp_get_nav_menu_items', 'wpturbo_auto_add_subpages_to_menus', 10, 2 );
				

The code snippet above adds all subpages of a page to a specific menu in WordPress. This snippet requires a basic understanding of PHP and WordPress. Let’s dive into how this code works.

First, we define and name our function: wpturbo_auto_add_subpages_to_menus(). This function is used to create and display the subpages within your specified menu. We then pass in two parameters $menu_items and $args. $menu_items refers to your current menu and $args is used to specify which menu you’d like to edit.

The if statement ( 'main-menu' !== $args->menu_slug ) ensures that we are only manipulating the menu we intended to. In the example code, the menu we are targeting is called ‘main-menu,’ which is specified in this if statement.

Next, we define the $new_items array that will store our new menu items. We set the $menu-count variable to the total amount of current menu items in the $menu-items array.

We use a foreach loop to loop through each menu item and then check if the object property of the menu_item is a page. If it is, we use the get_pages() function to retrieve all subpages associated with that page.

In the inner foreach loop, we loop through each subpage, create a new menu item, and add it to the $new_items array. We set the ID of the new menu item to $menu_count, add the parent ID of the current menu item, the subpage’s title, URL, the page order, and finally the object and object_id properties of the subpage.

Once we have looped through all subpages, we return our new menu items as an array to be used in our menu. We hook our function into the wp_get_nav_menu_items filter with add_filter() and pass in the parameters for our function.

And that’s all there is to it! You can use this code to automatically add subpages to one or more menus in WordPress.

Register an account to save your snippets or go Pro to get more features.