0

I want the top nav on a wordpress site to automatically show the child pages.

I know that I can edit my menu and add the children, but I want them to appear automatically, and for the menu to update if/when I change or add child pages.

I've partially achieved this using the code on this post: Add child pages automatically to nav menu

It works perfectly except for one thing... It lists the child pages alphabetically instead of the order that you select when creating the pages.

How can I get the menu to show the children in the correct order?

5
  • Are you asking how to sort by menu order with get_pages() ?
    – birgire
    Feb 18, 2016 at 10:16
  • I think so... the code in the answer linked above uses something along the lines of $children = get_pages Feb 18, 2016 at 10:20
  • 1
    Did you try array( 'sort_column' => 'menu_order', 'child_of' => $i->object_id) ) as suggested in the Codex ?
    – birgire
    Feb 18, 2016 at 10:23
  • You are an absolute genius, thank you, it is now working perfectly!!! Feb 18, 2016 at 10:39
  • 1
    Good to hear it's working for you, You could add an answer and accept it, to close the question, explaining it in more detail. It's also better to include all the relevant code into the question itself, instead of just linking to it.
    – birgire
    Feb 18, 2016 at 10:42

1 Answer 1

0

So this is the way I eventually achieved this.

I put the following code into my functions.php (actually into the functions.php of my child theme to keep it safe from updates) It has just one change to the original code from the link in the question thanks to @birgire.

/**
* auto_child_page_menu
* 
* class to add top level page menu items all child pages on the fly
* @author Ohad Raz <admin@bainternet.info>
* with a tweak by birgire <https://wordpress.stackexchange.com/users/26350/birgire>
*/
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( 'sort_column' => 'menu_order', 'child_of' => $i->object_id) ); //fetching the child pages according to the order set on the page
        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; //probably not right
            $c->current_item_parent   = ($post->ID == $c->post_parent)? true: false;
            $tmp[] = $c;
        }
    }
    return $tmp;
}
}
new auto_child_page_menu();

Thx everyone for your help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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