Blog Single

02 Aug

WordPress menu order change by pages order bootstrap menu

This code need to write in function.php

/* Elk Menu */
function elk_wp_menus( $args = array() ) {
$defaults = array('sort_column' => 'menu_order', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
$args = wp_parse_args( $args, $defaults );

/**
* Filter the arguments used to generate a page-based menu.
*
* @since 2.7.0
*
* @see wp_page_menu()
*
* @param array $args An array of page menu arguments.
*/
$args = apply_filters( 'wp_page_menu_args', $args );

$menu = '';

$list_args = $args;

// Show Home in the menu
if ( ! empty($args['show_home']) ) {
if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
$text = __('Home');
else
$text = $args['show_home'];
$class = '';
if ( is_front_page() && !is_paged() )
$class = 'class="current_page_item"';
$menu .= '

  • ' . $args['link_before'] . $text . $args['link_after'] . '
  • ';
    // If the front page is a page, add it to the exclude list
    if (get_option('show_on_front') == 'page') {
    if ( !empty( $list_args['exclude'] ) ) {
    $list_args['exclude'] .= ',';
    } else {
    $list_args['exclude'] = '';
    }
    $list_args['exclude'] .= get_option('page_on_front');
    }
    }

    $list_args['echo'] = false;
    $list_args['title_li'] = '';
    $menu .= str_replace( array( "\r", "\n", "\t" ), '', elk_wp_list_pages($list_args) );

    if ( $menu )
    $menu = '

    ';

    $menu = '

    ' . $menu . "

    \n";

    /**
    * Filter the HTML output of a page-based menu.
    *
    * @since 2.7.0
    *
    * @see wp_page_menu()
    *
    * @param string $menu The HTML output.
    * @param array $args An array of arguments.
    */
    $menu = apply_filters( 'wp_page_menu', $menu, $args );
    if ( $args['echo'] )
    echo $menu;
    else
    return $menu;
    }
    function elk_wp_list_pages( $args = '' ) {
    $defaults = array(
    'depth' => 0, 'show_date' => '',
    'date_format' => get_option( 'date_format' ),
    'child_of' => 0, 'exclude' => '',
    'title_li' => __( 'Pages' ), 'echo' => 1,
    'authors' => '', 'sort_column' => 'menu_order, post_title',
    'link_before' => '', 'link_after' => '', 'walker' => new elk_Walker(),
    );

    $r = wp_parse_args( $args, $defaults );

    $output = '';
    $current_page = 0;

    // sanitize, mostly to keep spaces out
    $r['exclude'] = preg_replace( '/[^0-9,]/', '', $r['exclude'] );

    // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
    $exclude_array = ( $r['exclude'] ) ? explode( ',', $r['exclude'] ) : array();

    /**
    * Filter the array of pages to exclude from the pages list.
    *
    * @since 2.1.0
    *
    * @param array $exclude_array An array of page IDs to exclude.
    */
    $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );

    // Query pages.
    $r['hierarchical'] = 0;
    $pages = get_pages( $r );

    if ( ! empty( $pages ) ) {
    if ( $r['title_li'] ) {
    $output .= '

    ';
    }
    }

    /**
    * Filter the HTML output of the pages to list.
    *
    * @since 1.5.1
    *
    * @see wp_list_pages()
    *
    * @param string $output HTML output of the pages list.
    * @param array $r An array of page-listing arguments.
    */
    $html = apply_filters( 'wp_list_pages', $output, $r );

    if ( $r['echo'] ) {
    echo $html;
    } else {
    return $html;
    }
    }
    class elk_Walker extends Walker_Page {
    public function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat( "\t", $depth );
    $output .= "\n$indent

      \n";
      }
      function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
      if ( $depth )
      $indent = str_repeat("\t", $depth);

      else
      $indent = '';
      // extract($args, EXTR_SKIP);
      $css_class = array('page_item', 'page-item-'.$page->ID);
      if ( !empty($current_page) ) {
      $_current_page = get_post( $current_page );
      if ( in_array( $page->ID, $_current_page->ancestors ) )
      $css_class[] = 'current_page_ancestor';
      if ( $page->ID == $current_page )
      $css_class[] = 'current_page_item';
      elseif ( $_current_page && $page->ID == $_current_page->post_parent )
      $css_class[] = 'current_page_parent';
      }
      elseif ( $page->ID == get_option('page_for_posts') ) {
      $css_class[] = 'current_page_parent';
      }

      $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
      $icon_class = get_post_meta($page->ID, 'icon_class', true); //Retrieve stored icon class from post meta

      $output .= $indent . '

    • ';
      $output .= '' . $link_before;

      if($icon_class){ //Test if $icon_class exists
      $output .= ''; //If it exists output a span with the $icon_class attached to it
      }

      $output .= apply_filters( 'the_title', $page->post_title, $page->ID );

      $output .= $link_after . '';

      if ( !empty($show_date) ) {
      if ( 'modified' == $show_date )
      $time = $page->post_modified;
      else
      $time = $page->post_date;
      $output .= " " . mysql2date($date_format, $time);
      }
      }
      }
      function add_parent_class_nav( $css_class, $page, $depth, $args )
      {
      if (!empty($args['has_children']))
      $css_class[] = 'dropdown';
      return $css_class;
      }
      add_filter( 'page_css_class', 'add_parent_class_nav', 10, 4 );

      After above code then need write elk_wp_menus function where you want to display this menu (in menu position )

      $args = array(
      'depth' => 2,
      'sort_column' => 'menu_order',
      'menu_class' => 'collapse navbar-collapse navbar-ex1-collapse',
      'include' => '',
      'exclude' => '1,2',
      'echo' => true,
      'show_home' => false,
      'link_before' => '',
      'link_after' => '' );

      elk_wp_menus( $args );
      ?>

      Also if you want multiple column by sub menu then need add this jquery

      jQuery(function(){
      var lis = $(".elk-mega > li");
      for(var i = 0; i < lis.length; i+=10) { lis.slice(i, i+10).wrapAll("

    Related Posts