Wordpress Custom Menu For Different Category/Page/Post


/ Published in: PHP
Save to your folder(s)

[register_nav_menus](http://codex.wordpress.org/Function_Reference/register_nav_menus,"register_nav_menus")
[wp_nav_menu](http://codex.wordpress.org/Function_Reference/wp_nav_menu,"wp_nav_menu")
[in_category](http://codex.wordpress.org/Function_Reference/in_category,"in_category")
[is_category](http://codex.wordpress.org/Function_Reference/is_category,"is_category")


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // add the following to wp theme function.php
  4. register_nav_menus( array(
  5. 'primary-menu' => 'main menu',
  6. 'marketing-menu' => 'marketing menu',
  7. 'shopping menu' => 'shopping menu',
  8. 'technology menu' => 'technology menu',
  9. 'recreation menu' => 'recreation menu',
  10. ) );
  11.  
  12. add_action( 'init', 'register_nav_menus' );
  13. ?>
  14.  
  15. <?php
  16. // add following to where you want the menu share, in my case, hearder.php
  17. <?php
  18. if((has_nav_menu('marketing-menu') and !is_home() and is_category(array(5,6,8,9,10,11,12,13,14))) or (has_nav_menu('marketing-menu') and !is_home() and in_category(array(5,6,8,9,10,11,12,13,14)))) {
  19. wp_nav_menu(array(
  20. 'theme_location' => 'marketing-menu',
  21. 'container' => '',
  22. 'menu_id' => 'primary-nav',
  23. 'container_class' => 'main-menu',
  24. 'menu_class' => 'nav'
  25. ));
  26. }
  27. else(has_nav_menu('primary-menu')){
  28. wp_nav_menu(array(
  29. 'theme_location' => 'primary-menu',
  30. 'container' => '',
  31. 'menu_id' => 'primary-nav',
  32. 'container_class' => 'main-menu',
  33. 'menu_class' => 'nav'
  34. ));
  35.  
  36. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.