Title: Switching Navigation Menu
Last modified: August 19, 2016

---

# Switching Navigation Menu

 *  [gs_s](https://wordpress.org/support/users/gs_s/)
 * (@gs_s)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/)
 * Hi there,
 * I’m wondering if it is possible to show tabbed menus on the sidebar. I mean the
   visitors see a few options to choose the navigation menu styles. Something like
   this, let’s say there are two menus, Directory Listing and Alpabetical order.
   When the former is selected it looks like this,
 * > **Directory listing**| Alphabetical order
   >  Category A — Page 7 — Page 8 Category
   > C — Page 3 — Page 6 Category B — Page 4 Category D Catefory E — Page 1 — Page
   > 2
 * And if the latter is chosen, it’s gonna be like this,
 * > Directory listing|** Alphabetical order**
   >  Page 1 Page 2 Page 3 Page 4 Page
   > 5 Page 6 Page 7 Page 8
 * I’d like to achieve this without javascript. If anybody has any idea or information,
   please let me know. Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731413)
 * Don’t know of the javascript non-requrement but might look at [http://wordpress.org/extend/plugins/tabbed-widgets/](http://wordpress.org/extend/plugins/tabbed-widgets/)
 * Also look at [Otto’s PHP Code Widget](http://wordpress.org/extend/plugins/php-code-widget/)
   and put these two sets of code in separate widgets.
 *     ```
       <?php
       //get categories then display all posts in each term
       $taxonomy = 'category';//  e.g. post_tag, category
       $param_type = 'category__in'; //  e.g. tag__in, category__in
       $term_args=array(
         'orderby' => 'name',
         'order' => 'ASC'
       );
       $terms = get_terms($taxonomy,$term_args);
       if ($terms) {
         foreach( $terms as $term ) {
           $args=array(
             "$param_type" => array($term->term_id),
             'post_type' => 'post',
             'post_status' => 'publish',
             'posts_per_page' => -1,
             'caller_get_posts'=> 1
             );
           $my_query = null;
           $my_query = new WP_Query($args);
           if( $my_query->have_posts() ) {
             echo 'List of Posts in '.$taxonomy .' '.$term->name;
             while ($my_query->have_posts()) : $my_query->the_post(); ?>
               <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
             endwhile;
           }
         }
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *     ```
       <?php
       //alpha list of posts
       $args=array(
         'order' => 'ASC',
         'orderby' => 'title',
         'post_type' => 'post',
         'post_status' => 'publish',
         'posts_per_page' => -1,
         'caller_get_posts'=> 1
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
         endwhile;
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  Thread Starter [gs_s](https://wordpress.org/support/users/gs_s/)
 * (@gs_s)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731479)
 * Thanks MichaelH, for the reply.
 * Tabbed Widgets does exactly what I’ve been looking for except with javascript.
 * The example you wrote really gives me much understanding in depth of how I need
   to modify the theme. I really appreciate it. On the other hand, I need to list
   up WordPress pages, not blog posts. Currently I have assigned categories to the
   pages with a plugin called, [Ninja Page Categories and Tags](http://wordpress.org/extend/plugins/ninja-page-categories-and-tags/).
   Still I need more research on how I can retrieve those pages with specified categories.
 * I wonder if you have any ideas about it.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731480)
 * Can’t test that right now but
 *     ```
       'post_type' => 'post',
       ```
   
 * for pages would be
 *     ```
       'post_type' => 'page',
       ```
   
 *  Thread Starter [gs_s](https://wordpress.org/support/users/gs_s/)
 * (@gs_s)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731482)
 * Wow, great!
 * It worked like a charm for the second code starting with “//alpha list of posts”.
   But the first code starting with “//get categories” only lists up the pages in
   the default category and other categories do not show up.
 * Is it possible to retrieve all the defined categories and their pages? And hopefully
   subcategories are indented like this,
 * > Category A
   >  — Page 7 — Page 8 — Category C —- Page 3 —- Page 6 Category B —
   > Page 4
 * I’m afraid I’m asking too much. I hope it doesn’t bother you.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731507)
 * In this case, you have to get all categories, even those categories that have
   no Posts (yes Posts). So your $args for the get_terms needs to be:
 *     ```
       $term_args=array(
         'hide_empty' => false,
         'orderby' => 'name',
         'order' => 'ASC'
       );
       ```
   
 *  Thread Starter [gs_s](https://wordpress.org/support/users/gs_s/)
 * (@gs_s)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731529)
 * OMG, it works, Michael.
 * Also, just changing ‘orderby’ => ‘name’ to ‘orderby’ => ‘ID’ made it sort by 
   id.
 * However, it seems the post(page) order is somehow reversed like this, (the category
   order is fine.)
 * > Category A
   >  — Page 3 — Page 2 — Page 1 Category B — Page 5 — Page 4 Category
   > C — Page 2 — Page 7 — Page 6
 * Also pages with multiple categories do not get sorted. in this example it is ‘
   Page 2’. I’d like to change it so that it looks like this,
 * >  Category A
   >  — Page 1 — Page 2 — Page 3 Category B — Page 4 — Page 5 Category
   > C — Page 2 — Page 6 — Page 7
 * So in order to achieve this, I tried changing this part but with no luck.
 *     ```
       $args=array(
           'orderby'            => 'name',
           'order'              => 'DESC',
             "$param_type" => array($term->term_id),
             'post_type' => 'page',
             'post_status' => 'publish',
             'posts_per_page' => -1,
             'caller_get_posts'=> 1
             );
       ```
   
 * _(I’m talking about the first block of code you wtote starting with //get categories.)_
 * I also tried using `asort($my_query->have_posts() );` but the error message said
   $my_query->have_posts() is not an array. OK, so if I want to sort and reverse
   the order, It seems I have to put them into an array first. After that, I can
   reverse with php sort functions.
 * The following code is what I got so far. After inserting this code, my web server
   stoped working. So I cannot test it anymore until it recovers. I hope my code
   is not responsible for the server down.
 *     ```
       <ul>
       <?php
       //get categories then display all posts in each term
       $taxonomy = 'category';		//  e.g. post_tag, category
       $param_type = 'category__in'; //  e.g. tag__in, category__in
       $term_args=array(
         'hide_empty' => false,
         'orderby' => 'ID',
         'order' => 'ASC'
       );
       $terms = get_terms($taxonomy,$term_args);
       if ($terms) {
         foreach( $terms as $term ) {
           $args=array(
             "$param_type" => array($term->term_id),
             'post_type' => 'page',
             'post_status' => 'publish',
             'posts_per_page' => -1,
             'caller_get_posts'=> 1
             );
           $my_query = null;
           $my_query = new WP_Query($args);
           if( $my_query->have_posts() ) {
             echo $term->name;
             $i = 0;
             while ($my_query->have_posts()) : $my_query->the_post() {
       		  $permalink_array[$i] = the_permalink();
       		  $title_att_array[$i] = the_title_attribute();
       		  $the_title_array[$i] = the_title();
       		  $i++;
             }
             while ($i) : ?>
   
               <li><a href="<?php echo $permalink_array[$i]; ?>" rel="bookmark" title="Permanent Link to <?php echo $title_att_array[$i]; ?>"><?php echo $the_title_array[$i]; ?></a></li>
              <?php
             $i--;
             endwhile;
           }
         }
       }
       ?>
       </ul>
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731530)
 * Add `'order' => 'ASC',`
 * to this:
 *     ```
       $args=array(
             "$param_type" => array($term->term_id),
             'post_type' => 'page',
             'post_status' => 'publish',
             'posts_per_page' => -1,
             'caller_get_posts'=> 1
       ```
   
 *  Thread Starter [gs_s](https://wordpress.org/support/users/gs_s/)
 * (@gs_s)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731532)
 * Thanks, Michael!
 * (By the way my server has recovered. It seems my code did something wrong to 
   the server. :))
 * Adding ‘order’ => ‘ASC’ worked almost perfectly. Still the pages with multiple
   categories do not get sorted by name.
 * Let’s say “page D” in the example below is assigned to Category1 and Caegory2
   and it looks like this,
 * > Category 1
   >  — Page D (page ID 1) — Page E (page ID 2) — Page F (page ID 3) 
   > Category 2 — Page D (page ID 1) — Page A (page ID 4) — Page B (page ID 5) Category
   > 3 — Page C (page ID 6) — Page G (page ID 7) — Page H (page ID 8)
 * They seem to be soted by ID, so I added `'orderby' => 'name',` under `$args=array(`
   but it didn’t work.
 *     ```
       foreach( $terms as $term ) {
           $args=array(
             'orderby' => 'name',
       ```
   
 * The below code reverses the category order. So, I wonder this kind trick can 
   be applied to the listed pages.
 *     ```
       $terms = get_terms($taxonomy,$term_args);
       rsort($terms);
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731534)
 * If the categories are in the wrong order then you would change the $term_args.
   If the posts are in the wrong order within each category then add ‘orderby’ =
   > ‘title’ to the $args list.
 * See [http://codex.wordpress.org/Template_Tags/query_posts](http://codex.wordpress.org/Template_Tags/query_posts)
   for additional arguments.
 *  Thread Starter [gs_s](https://wordpress.org/support/users/gs_s/)
 * (@gs_s)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731537)
 * Thanks, Michael!
 * `'orderby' => 'title'` just did the job. I cannot believe I could have done this
   far. I really appreciate it. This is gonna be the last question hopefully.
 * Now I’m trying to indent the subcategories looking like this,
 * > Category 1
   >  — Page D — Page E — Page F — Category 3 —- Page C —- Page G —- 
   > Page H Category 2 — Page A — Page B — Page D
 * `get_category_parents()` seem to evaluate if the current category has a parent
   category or not. So, I inserted `if (!get_category_parents($term->id, False))
   echo "<ul>";` before `while ($my_query->have_posts()) : $my_query->the_post();`
   and `if (!get_category_parents($term->id, False)) echo "</ul>";` after `<?php
   the_title(); ?></a></li>` But I don’t see any changes in the actual web page.
   I also tried `get_category_parents($term->name, False)` but it doesn’t work either.
   Do you see what I am missing?
 *     ```
       <ul>
       <?php
       //get categories then display all posts in each term
       $taxonomy = 'category';		//  e.g. post_tag, category
       $param_type = 'category__in'; //  e.g. tag__in, category__in
       $term_args=array(
         'hide_empty' => false,
         'orderby' => 'ID',
         'order' => 'ASC'
       );
       $terms = get_terms($taxonomy,$term_args);
       if ($terms) {
         foreach( $terms as $term ) {
           $args=array(
             'orderby' => 'title',
             'order' => 'ASC',
             "$param_type" => array($term->term_id),
             'post_type' => 'page',
             'post_status' => 'publish',
             'posts_per_page' => -1,
             'caller_get_posts'=> 1
             );
           $my_query = null;
           $my_query = new WP_Query($args);
   
           if( $my_query->have_posts() ) {
             echo $term->name;
       	if (!get_category_parents($term->id, False))
       		echo "<ul>";
             while ($my_query->have_posts()) : $my_query->the_post(); ?>
               <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
              <?php
             endwhile;
           if (!get_category_parents($term->id, False))
       		echo "</ul>";
           }
         }
       }
       ?>
       </ul>
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Switching Navigation Menu’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 2 participants
 * Last reply from: [gs_s](https://wordpress.org/support/users/gs_s/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/switching-navigation-menu/#post-1731537)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
