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.

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?

share|improve this question

migrated from stackoverflow.com May 27 '13 at 7:22

This question came from our site for professional and enthusiast programmers.

    
The only way I know is when your custom menu is empty, WordPress will fill it with all existing pages. Once you start adding pages inside it, it won't continue automatically. –  Alexandre Lavoie May 27 '13 at 5:22
    
It looks like you've got a problem with configuring a software-component. Please contact the vendor of that component for your support options, this website here is about concrete programming questions, not how to use a vendor-specific API. –  hakre May 27 '13 at 6:40
    
Thanks for migrating Toscho - I didn't know this forum existed. –  Desmond May 28 '13 at 0:59
    
code for menu with sub-menu from pages and child pages.It MAY help you build your menu from child pages and child pages pragmatically. –  Core May 28 '13 at 6:37

1 Answer 1

up vote 2 down vote accepted

here is how:

/**
* 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();
share|improve this answer
    
That works perfectly, thanks so much for your help Bainternet! Much appreciated. –  Desmond May 29 '13 at 11:13
    
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? –  1GR3 May 29 at 16:14

protected by Community May 29 at 16:23

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.