By Desmond


2013-05-27 05:17:06 8 Comments

I have built a custom menu in Wordpress that contains links to posts and pages. I am adding it to my header using this line of code:

<?php 

wp_nav_menu(
    array(
        'theme_location' => 'primary', 
        'depth'          => 0, 
        'menu_class'     => 'nav-menu',
    ) 
);

?>

My problem is, if I add a child page to the top level ones on the menu, they don't automatically appear as sublinks on the nav. I know I can create them manually each time by rebuilding the menu, but I would like to be able to just add a child page in the pages section and have it show up in the nav without having to go to the menu and build it there also, if that makes sense?

I've tried using depth => 0, but that didn't work. Is there a way to have child pages show up without having to build it into the custom menu?

1 comments

@Bainternet 2013-05-27 09:05:41

here is how:

/**
* auto_child_page_menu
* 
* class to add top level page menu items all child pages on the fly
* @author Ohad Raz <[email protected]>
*/
class auto_child_page_menu
{
    /**
     * class constructor
     * @author Ohad Raz <[email protected]>
     * @param   array $args 
     * @return  void
     */
    function __construct($args = array()){
        add_filter('wp_nav_menu_objects',array($this,'on_the_fly'));
    }
    /**
     * the magic function that adds the child pages
     * @author Ohad Raz <[email protected]>
     * @param  array $items 
     * @return array 
     */
    function on_the_fly($items) {
        global $post;
        $tmp = array();
        foreach ($items as $key => $i) {
            $tmp[] = $i;
            //if not page move on
            if ($i->object != 'page'){
                continue;
            }
            $page = get_post($i->object_id);
            //if not parent page move on
            if (!isset($page->post_parent) || $page->post_parent != 0) {
                continue;
            }
            $children = get_pages( array('child_of' => $i->object_id) );
            foreach ((array)$children as $c) {
                //set parent menu
                $c->menu_item_parent      = $i->ID;
                $c->object_id             = $c->ID;
                $c->object                = 'page';
                $c->type                  = 'post_type';
                $c->type_label            = 'Page';
                $c->url                   = get_permalink( $c->ID);
                $c->title                 = $c->post_title;
                $c->target                = '';
                $c->attr_title            = '';
                $c->description           = '';
                $c->classes               = array('','menu-item','menu-item-type-post_type','menu-item-object-page');
                $c->xfn                   = '';
                $c->current               = ($post->ID == $c->ID)? true: false;
                $c->current_item_ancestor = ($post->ID == $c->post_parent)? true: false; //probbably not right
                $c->current_item_parent   = ($post->ID == $c->post_parent)? true: false;
                $tmp[] = $c;
            }
        }
        return $tmp;
    }
}
new auto_child_page_menu();

@Desmond 2013-05-29 11:13:04

That works perfectly, thanks so much for your help Bainternet! Much appreciated.

@1GR3 2014-05-29 16:14:16

Sorry for asking that but, how should I use this? paste it in functions.php and call it from the place where menu suppose to go?

@Spedwards 2015-03-01 05:16:37

I'm with @1GR3, how do we use this?

@David Shaw 2018-01-23 15:10:30

Yeah, simply add this to the functions.php file and press save!

@Joe Bloggs 2019-05-12 01:33:30

Good effort, but this DOES NOT WORK! It adds ALL the child pages under the top level parent as they would be 1st level children. The 2nd and 3rd level child pages are also added under top level parent. Totally messed up my navigation.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Add (grand)child pages automatically to nav menu

  • 2018-04-26 09:00:48
  • MagpieMachine
  • 223 View
  • 0 Score
  • 1 Answer
  • Tags:   menus pages sub-menu

1 Answered Questions

2 Answered Questions

[SOLVED] Use wp_nav_menu to dynamically generate child menus

  • 2013-06-25 16:43:43
  • danbrellis
  • 4276 View
  • 0 Score
  • 2 Answer
  • Tags:   menus walker

2 Answered Questions

[SOLVED] Automatically add child pages to menu

  • 2014-02-05 08:48:16
  • Josh
  • 1427 View
  • 0 Score
  • 2 Answer
  • Tags:   plugins menus

1 Answered Questions

"Static" Child Menu with Accordian

  • 2014-01-30 20:48:42
  • Nathan S
  • 313 View
  • 0 Score
  • 1 Answer
  • Tags:   menus children

1 Answered Questions

[SOLVED] Make Theme automatically choose default nav menu

1 Answered Questions

[SOLVED] Styling child custom menu pages

  • 2011-08-09 12:42:30
  • Toby
  • 107 View
  • 0 Score
  • 1 Answer
  • Tags:   menus css

Sponsored Content