Advertisement
deepbevel

Untitled

Feb 15th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. // Menu items and corresponding category IDs
  2. $menu_items_to_categorise = array(10 => 1, 11 => 2, 12 => 3);
  3.  
  4. // Get current URL
  5. $current_url = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  6.  
  7. // Get the WordPress generated menu
  8. $nav_menu = wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'echo' => 0 ) );
  9.  
  10. // Loop through the items to categorise
  11. foreach ($menu_items_to_categorise as $menu_item_number => $menu_item_cat) {
  12.  
  13. // Get the posts for the current menu-item number
  14. global $post;
  15. $tmp_post = $post;
  16. $args = array( 'category' => $menu_item_cat );
  17. $myposts = get_posts( $args );
  18.  
  19. // Create list
  20. $posts_sub_menu = "\n" . '<ul class="sub-menu">' . "\n";
  21.  
  22. // Loop through the posts and append the list entries
  23. foreach( $myposts as $post ) :
  24. setup_postdata($post);
  25. $posts_sub_menu .= '<li class="menu-item menu-item-type-custom menu-item-object-custom' . (get_permalink() == $current_url ? ' current-menu-item' : '' ) . '"><a href="'. get_permalink() . '">' . get_the_title() . "</a></li>\n";
  26. endforeach;
  27. $posts_sub_menu .= "</ul>\n";
  28.  
  29. // Return to the parent loop so there will be no conflict
  30. $post = $tmp_post;
  31.  
  32. // Inject created list into existing menu
  33. $pattern = "%(<li [^>]*id=\"menu-item-" . $menu_item_number . "\"[^>]*>.*)(</li>)%mU";
  34. $replacement = '${1}' . $posts_sub_menu . '$2';
  35. $nav_menu = preg_replace($pattern, $replacement, $nav_menu);
  36. }
  37. unset($menu_item_number);
  38. unset($menu_item_cat);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement