• Below is the code I am using which creates navigation menu dynamically.

    I am trying to edit the code so that each menu button has a different colour.

    I can see where has_nav_menu comes in to create the necessary standards in the array (‘menu_id’ gets set to ‘dropmenu’). How can I set it to different divs eg ‘dropmenu-art’ which are already set up in CSS. Alternatively I could just change background color in If loop below?

    Please help!

    <?php
    	//NORMAL MENU
    	if (has_nav_menu( 'main' ) ) { wp_nav_menu(array('theme_location' => 'main', 'container_id' => 'navigation', 'menu_id' => 'dropmenu')); }
    	//SELECT MENU
    	$menu_name = 'main';
    
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    		$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    		$menu_items = wp_get_nav_menu_items($menu->term_id);
    		$menu_list = '<select id="selectMenu"><option value="" selected="selected">'.__('Menu','themolitor').'</option>';
    
    		foreach ( (array) $menu_items as $key => $menu_item ) {
    	    	$title = $menu_item->title;
    	   		$url = $menu_item->url;
    	   		$menu_list .= '<option value="' . $url . '">' . $title . '</option>';
    
    			/* -- set menu colours -- */
    			if ($title == "Art") {
    					/* -------- How do I change "menu_id' from 'dropmenu' to 'dropmenu-art'  ????? ----------*/
    				} else if ($title == "Events") {
    					/*   perhaps there is a method to just change CSS "background: #000;" property here instead?   */
    				} else if ($title == "Influences") {
    					/*   etc   */
    				} else if ($title == "Links") {
    					/**/
    				} else if ($title == "Personal") {
    					/**/
    				} else if ($title == "Portraits") {
    					/**/
    				} else if ($title == "Press") {
    					/**/
    				} else if ($title == "Software") {
    					/**/
    				} else if ($title == "Technical") {
    					/**/
    				} else if ($title == "Video") {
    					/**/
    				} else {
    					/**/
    			}
    		}
    		$menu_list .= '</select>';
    		echo $menu_list;
        }
        ?>
  • The topic ‘different colors for nav buttons using CSS and PHP’ is closed to new replies.