将父页面的子页面添加到导航栏PHP

时间:2016-07-29 作者:Acevandermeer

我在一个自定义主题中使用了一个函数,它会自动用每个父级的所有子页面填充导航。我在上找到了密码this site

这很好,但这个函数对我来说唯一做不到的事情就是按照Wordpress中页面部分中设置的顺序对页面进行排序。我想我需要使用类似“sort\\u column=menu\\u order&;title\\u li=&;child\\u of=\'在下面的代码中的某个地方,但我一辈子都不能计算出在哪里?

/**
* 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();

2 个回复
最合适的回答,由SO网友:Acevandermeer 整理而成

我最终为任何有相同问题的人找到了解决方法:

  /**
  * 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, \'sort_column\' => \'menu_order\') );
              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();

SO网友:Krzysztof Grabania

$items 传递给此函数的已排序(请参阅here). 所以你必须在return $tmp;