1

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
1
  • 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, 2014 at 22:37

2 Answers 2

0

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')
    );

}
0

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
}
2
  • try changing "menu-primary-menu" with the numeric ID for your menu.
    – Will
    Feb 12, 2014 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. Feb 12, 2014 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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