• I am using WP 3.0.1. and loving the mew menu feature. So far I can put pages and categories on the menu. But now I want to have the category’s posts as dropdown menu (2nd – level menu). For example, I have a category called “Wine” and I have posts Cabernet, Merlot and Pinot Noir as the posts. I want the “Wine” appears on the menu, and as I roll over it the drop down menu shows up with Cabernet, Merlot and Pinot Noir. And as I add more posts within this category, more will be on the dropdown menu as well.

    Is there a way to do that?

Viewing 2 replies - 1 through 2 (of 2 total)
  • It doesn’t look like what you want to do is possible using posts.

    Is there any reason you’re not using Pages instead of posts?

    Hey JeffTD, I guess by now you’ve probably found a solution or just decided you’re not going to do things this way.

    For the interest of others who might need such a feature, here’s how I implemented it:

    // 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);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘dynamic posts in menu?’ is closed to new replies.