WordPress.org

Support

Support » Themes and Templates » [Resolved] WordPress 3.0 Menu class and id issues

[Resolved] WordPress 3.0 Menu class and id issues

  • I am trying to integrate the dynamic menu into my theme in progress. I have this code in the header to load the menu:

    <?php wp_nav_menu(array('theme_location' => 'primary', 'container_id' => 'navigation')); ?>

    which generates html with a container div with the standard menu class, but no container id even though I added it to the array:

    <div class="menu"><ul><li class="page_item page-item-439">
    <--------- cut ------------->
    
    </li></ul></div>

    NB in my theme’s functions.php I have:

    //add dynamic menu
    
    add_action( 'init', 'register_my_menus' );
    
    function register_my_menus() {
    	register_nav_menus(
    		array(
    			'primary-menu' => __( 'Primary Menu' ),
    			'secondary-menu' => __( 'Secondary Menu' ),
    			'tertiary-menu' => __( 'Tertiary Menu' )
    		)
    	);
    }

    And I created a menu called primary with all my pages in the backend.

    Why can’t I add the div container id I want? How can I add an id to the ul?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Just rearranged the primary menu in the backend and reloaded the front page. No change so I guess the standard wp_page_menu is loaded. SO another question is here why the primary menu is not loaded.

    Update: Never mind this remark. had to adjust the array for the dynamic menu in the header:

    <?php wp_nav_menu(array('theme_location' => 'primary-menu', 'container_id' => 'navigation')); ?>

    Now I only need to find out how to add a class to an ul and why the id is not added to the container..

    Fixed it all already 🙂 :

    <?php wp_nav_menu(array('theme_location' => 'primary-menu', 'container_id' => 'navigation', 'menu_class' => 'nav')); ?>

    added the class I needed and as soon as the proper menu widget was loaded, the container id got loaded as well. Now I only need to make sure the menu is displayed OK, when there is not menu widget..

    I am trying an if else statement, but there is a PHP “Unexcpected T_ELSE” parse error:

    <?php if ( has_nav_menu( 'primary-menu' ) ) { ?>
    			  <?php wp_nav_menu(array('theme_location' => 'primary-menu', 'container_id' => 'navigation', 'menu_class' => 'nav')); ?>
    		<?php } ?>
    			<?php else { ?>
    				<div id="navigation"><ul id="nav">
    				<li class="page_item <?php if (is_front_page()) echo( 'current_page_item');?>"><?php wp_list_pages( 'title_li=' ); ?>
    					</ul></div><!--end navigation-->
    
    		<?php	} ?>

    Anyone?

    Fixed the if else as well it seems:

    <?php if ( has_nav_menu( 'primary-menu' ) ) { ?>
    						  <?php wp_nav_menu(array('theme_location' => 'primary-menu', 'container_id' => 'navigation', 'menu_class' => 'nav')); ?>
    					<?php }
    						 else { ?>
    							<div id="navigation"><ul class="nav">
    							<li class="page_item <?php if (is_front_page()) echo( 'current_page_item');?>"><?php wp_list_pages( 'title_li=' ); ?>
    								</ul></div><!--end navigation-->
    
    					<?php	} ?>

    Now the standard wp_list_pages menu will run and will be styled properly if no dynamic menu is present.

    One more thing. When you use this last piece of code in a 2.9.2 setting you get

    Fatal error: Call to undefined function register_nav_menus() in /opt/local/apache2/htdocs/wordpress/wp-content/themes/d3/functions.php  on line 472

    This even though we have used “if has_nav_menu” in the header file.
    Therefore you need to add an if function exists: http://php.net/manual/en/function.function-exists.php to the code that loads the menu in functions.php :

    if (function_exists ('register_my_menus')) {
    
    function register_my_menus() {
    	register_nav_menus(
    		array(
    			'primary-menu' => __( 'Primary Menu' ),
    			'secondary-menu' => __( 'Secondary Menu' ),
    			'tertiary-menu' => __( 'Tertiary Menu' )
    		)
    	);
    }
    }

    Then I do still get a warning:

    warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'register_my_menus' not found or invalid function name in /opt/local/apache2/htdocs/wordpress/wp-includes/plugin.php  on line 339

    That line has:

    call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));

    but most servers PHP Configuration should not show those warnings.

    Thank you for posting all of this, it’s coming in very handy right now.

    You’re welcome. We are here to help each other after all.

    @rhand, much appreciated. Ran into the issue

    warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'register_my_menus' not found or invalid function name in /opt/local/apache2/htdocs/wordpress/wp-includes/plugin.php on line 339

    , and wrapped it in the function_exists check to get rid of her. Thanks much!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] WordPress 3.0 Menu class and id issues’ is closed to new replies.