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.

I have a website and I want to add automatically categories to my menu. I mean when I create new category it should appear in my menu. How I can make it?

  <?php wp_nav_menu( array( 'theme_location' => 'header', 'container' => '' ) ); ?>

Home | Categories | Contact
          category1
          category2
          category3
share|improve this question
    
Please show us your research, what you've done, where you've failed. More info on How to Ask a good question. –  kaiser Feb 10 at 22:37

2 Answers 2

I added it to functions.php but nothing happend

add_action( 'created_category', 'my_update_category_menu' );
add_action( 'delete_category ', 'my_delete_category_menu' );

function my_update_category_menu ($cat) {
    $category = get_category($cat);
    $menu_id = 'menu-primary-menu'; // <--------- YOUR ID

    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __($category->name),
        'menu-item-classes' => $category->name,
        'menu-item-url' => home_url( get_category_link( $category->term_id ) ), 
        'menu-item-status' => 'publish')
    );

}
function my_delete_category_menu ($cat) {
    $category = get_category($cat);
    // not sure how to get the backwards look up but menu items are stored as posts 
    // http://codex.wordpress.org/Function_Reference/wp_delete_post
}
share|improve this answer
    
try changing "menu-primary-menu" with the numeric ID for your menu. –  Will Feb 12 at 1:04
    
I made it in diffrent way but how can I change the category menu item order. Now when I add it in functions by default it's last i want to move it to 2nd position. –  user3211948 Feb 12 at 8:40

Something along these lines should at least get your started. You'll need to register the menu first and get the ID. I didn't get to deleting categories but you'll need to take care of that as well.

This is largely untested. Back up your DB first!!

add_action( 'created_category', 'my_update_category_menu' );
add_action( 'delete_category ', 'my_delete_category_menu' );

function my_update_category_menu ($cat) {
    $category = get_category($cat);
    $menu_id = yourMenuIdHere; // <--------- YOUR ID

    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __($category->name),
        'menu-item-classes' => $category->name,
        'menu-item-url' => home_url( get_category_link( $category->term_id ) ), 
        'menu-item-status' => 'publish')
    );

}
function my_delete_category_menu ($cat) {
    $category = get_category($cat);
    // not sure how to get the backwards look up but menu items are stored as posts 
    // http://codex.wordpress.org/Function_Reference/wp_delete_post
}

If you want to bulk add existing categories (though it's pretty easy to do it manually via the admin) something along these lines should work. Run it once and remove it from your functions.php.

$categories = get_categories();

foreach($categories as $category) {

     wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __($category->name),
        'menu-item-classes' => $category->name,
        'menu-item-url' => home_url( get_category_link( $category->term_id ) ), 
        'menu-item-status' => 'publish')
    );

}
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.