Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Menu items and corresponding category IDs
- $menu_items_to_categorise = array(10 => 1, 11 => 2, 12 => 3);
- // Get current URL
- $current_url = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
- // Get the WordPress generated menu
- $nav_menu = wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'echo' => 0 ) );
- // Loop through the items to categorise
- foreach ($menu_items_to_categorise as $menu_item_number => $menu_item_cat) {
- // Get the posts for the current menu-item number
- global $post;
- $tmp_post = $post;
- $args = array( 'category' => $menu_item_cat );
- $myposts = get_posts( $args );
- // Create list
- $posts_sub_menu = "\n" . '<ul class="sub-menu">' . "\n";
- // Loop through the posts and append the list entries
- foreach( $myposts as $post ) :
- setup_postdata($post);
- $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";
- endforeach;
- $posts_sub_menu .= "</ul>\n";
- // Return to the parent loop so there will be no conflict
- $post = $tmp_post;
- // Inject created list into existing menu
- $pattern = "%(<li [^>]*id=\"menu-item-" . $menu_item_number . "\"[^>]*>.*)(</li>)%mU";
- $replacement = '${1}' . $posts_sub_menu . '$2';
- $nav_menu = preg_replace($pattern, $replacement, $nav_menu);
- }
- unset($menu_item_number);
- unset($menu_item_cat);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement