nav_menu_item_args duplicating children

I came across [this](https://advent.elliottrichmond.co.uk/automatically-add-child-pages-to-menus/) website that has a code snippet to automatically add children to top level pages called with wp\_nav\_menu(). It’s duplicating the children of the first top level page on the remaining top level pages. I’m expecting:

* Top Level Page 1
* Top Level Page 1 Sub 1
* Top Level Page 1 Sub 2
* Top Level Page 2
* Top Level Page **2** Sub 1
* Top Level Page **2** Sub 2

But what I’m getting is:

* Top Level Page 1
* Top Level Page 1 Sub 1
* Top Level Page 1 Sub 2
* Top Level Page 2
* Top Level Page **1** Sub 1
* Top Level Page **1** Sub 2

For the life of me, I can’t figure out why this is happening. The function is being called the correct number of times. The `$item` is correct each time. Yet somehow they all end up with the same children.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer