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.

Is it possible to create a menu with a category such that each time that I had a sub category that automatically display on the menu?

As far as I know, if I have a menu with the category "fruit", and I create the subcategory "bananas", I have to manually add it.

share|improve this question

5 Answers 5

To solve this problem i tried to create a plugin which automatically create menu item for publish category under it's parent category if in menu.

Idea for plugin:

1.in code, i used "created_$taxonomy" action hook.

2.collect all the menu item ( by using wp_get_nav_menu_items)

3.and put condition if parent of publish category lies in menu-item than a menu-item created as sub menu of it's parent category.(by using wp_update_nav_menu_item)

<?php
/*
Plugin Name: Auto Category Submenu
Plugin URI: 
Description: Create menu item when category publish/create if it's parent category in menu item.
Version: 0.1
Author: Mayank Gupta/Ravinder Kumar
Author URI: 
License: GPL2
*/

class AutoSubmenu {

    /**
     * Constructor
     */
    function __construct() {
        add_action( 'created_category', array( &$this, 'on_publish_cat' ) );
    }

    /**
     * When publishing a new child page, add it to the appropriate custom menu.
     */
    function on_publish_cat( $cat_id ) {
        $cat = get_category( $cat_id );

        //menu id for you menus in which your sub-category's parent category as menu item 
        $menu_ids = array(101,100);

        //retrieve all meu items from menu
        foreach($menu_ids as $menu_id){
            $menu_items[] = wp_get_nav_menu_items( $menu_id, array('post_status' => 'publish','post_type' => 'nav_menu_item','output' => ARRAY_A, ) );
        }
        foreach($menu_items as $menu_item){
            $counter = 0 ;
            echo'<!--';
            print_r($menu_item);
            echo'-->';
            foreach($menu_item as $menu_term){
                $menu_terms_id[] = $menu_term->object_id; //all menu items id
            }
        }
        //print_r($menu_terms_id); //test for items in menu
        foreach( $menu_terms_id as $menu_term ){
            if( $cat->category_parent == $menu_term){
                wp_update_nav_menu_item( $cat_id, 0, array(
                'menu-item-object-id' => $cat_id,
                'menu-item-object' => $cat->taxonomy,
                'menu-item-parent-id' => $menu_term,
                'menu-item-type' => 'category',
                'menu-item-status' => 'publish'
            ) );
            }else{
                return;
            }
        }
    }

}
$auto_submenu = new AutoSubmenu();

Note: plugin giving error (header already sent) and menu item not created when category publish.This may be another topic for problem but i put it here because this may be a solution if some suggest me where i am wrong.Sorry if i'm doing something wrong by putting it here

share|improve this answer

This depends on what kind of menu you are talking about:

1)

If you are talking about "custom menus" (found in the Backend under Design -> Menus) you can do the following:

  • Create a new function with the action hook add_category

  • inside of this function, you can create a new post of type the menu item type, which is added correctly to your menu

  • whenever a new category is published the menu item is added now

  • you should also hook into delete category, to keep your menu up-to-date

2)

You could do much easier without a wordpress menu:

  • just write a function to list your categories (or use the predefined wp_list_categories function)
  • if this is not sufficient write a customized one which (maybe
    recursivley) reads your categories from the database

For option 1 to code you require some knowledge about WordPress though.

share|improve this answer

I prefer using wp_list_categories. Here is my code I used in my last project:

<?php wp_list_categories('orderby=ID&exclude=3,10,1,16,38&title_li=<span class="sidebar_heading d_shadow">' . __('Categories') . '</span>'); ?>

You will get a list of all your categories and sub-categories.

share|improve this answer

I coded my own for one my of clients

<div class="menu">
  <ul id="MenuBar1" class="MenuBarHorizontal">
    <li><a <?PHP if( count($_GET) == 0 ){ echo 'class="-current"';} ?> href="<?php echo esc_url( home_url( '/' ) ); ?>">home</a></li>
    <?php
        $category_ids = get_all_category_ids();
        sort($category_ids);
        foreach($category_ids as $cat_id) {
            $cat_name = get_cat_name($cat_id);

            echo '<li><a href="' . get_category_link( $cat_id ) . '">' . $cat_name . '</a>';            
            $args = array( 'numberposts' => -1, 'offset'=> 0, 'cat' => $cat_id, 'orderby' => 'ID', 'order' => 'ASCE' );     

            echo '<ul>';

            /* The 2nd Query (without global var) */
            $query2 = new WP_Query( $args );
            // The 2nd Loop
            while( $query2->have_posts() ):
                $query2->next_post();
                echo '<li><a href="' . get_permalink( $query2->post->ID ) . '">' . get_the_title( $query2->post->ID ) . '</a></li>';
            endwhile;

            // Restore original Query & Post Data
            wp_reset_query();
            wp_reset_postdata();
            echo '</ul>';
        echo '</li>';
        }
    ?>
  </ul>
share|improve this answer
    
dont think you closed div class menu –  Jon Feb 9 at 14:26

Another alternative would be to use List Custom Taxonomy Widget, it might work in your use case (if you are not planning to use WP nav_menu).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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