Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Here's the solution that @bainternet has posted here: Add child pages automatically to nav menu

/**
* auto_child_page_menu
* 
* class to add top level page menu items all child pages on the fly
* @author Ohad Raz <admin@bainternet.info>
*/
class auto_child_page_menu
{
    /**
     * class constructor
     * @author Ohad Raz <admin@bainternet.info>
     * @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 <admin@bainternet.info>
     * @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();

How to use it?

I have to implement it on a published website so I'm not in a position to experiment much...

c/p code to functions.php?

call the function from inside the header?

share|improve this question
    
You should really set yourself up with a local test site. I personally use XAMMP locally to test code and to develop themes. –  Pieter Goosen May 29 at 17:48
    
@PieterGoosen are you suggesting to try randomly pasting this code allover a test site? –  1GR3 May 29 at 17:52
    
No, this code goes into functions.php. To create a local test site, check this out –  Pieter Goosen May 29 at 17:56
    
Thank you @PieterGoosen! It wasn't that hard to answer, was it? –  1GR3 May 29 at 23:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.