How to create dynamic WordPress Menu or Nav

[easy-social-share counters=0 style="button"]

very first in your current theme you have open functions.php or create one, file you will need to create code in function to register the name of your current theme menu. ( This is the way they would be shown in the Appearance -> Menu admin display screen. )

For example, this menus would likely be shown in the “Theme Locations” box just as “Header Menu”.
writing the code in function.php file

    function register_my_menu() {
    register_nav_menu('header-menu',__( 'Header Menu' ));
    add_action( 'init', 'register_my_menu' );

This can makes how  two menus selections show, header menu or footer menu – Code is here

    function register_my_menus() {
          'header-menu' => __( 'Header Menu' ),
          'extra-menu' => __( 'Footer Menu' )
    add_action( 'init', 'register_my_menus' );

Shows Menu on Theme

When you have done just that, your current themes would be mostly all set. The end preparing stage is always to say to the themes the place where you will need the menu to display. This can be done in the important themes file folder. And so , for one example , we would want our header menus to be done in header.php. Now open that file folder in the themes editor, or choose the place where you need to set your own menus. The code to make use of there is wp_nav_menu which I will need to have one time for every single menus specific location. So that, insert this code –

<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>

All you are required to ensure that is always that the theme_location points to the title you presented to your current menus in the functions.php code above.

( Keep in mind that it will be the header-menu getting used right here other than Header Menu without a hyphen. header-menu is the title that the code knows about, Header Menu is the human-readable version that you will check in the admin web page. )

To finalized the code, you can still enter your current footer menu anyplace as well. You might will want a menu using one of your current page, as an example, so you could even require it to be jazzed up a little bit with a which contain DIV of a specific class –

wp_nav_menu( array( ‘theme_location’ => ‘extra-menu’, ‘container_class’ => ‘my_extra_menu_class’ ) );

So you would probably enter the above into your current Web page website template, rather than one may the menus display everywhere you add it, it’d be style of mainly because my_footer _menu_class to be able to make use of that in CSS.

Menu Section

This is all of the background work. To complete, you could just simply check out the Appearance -> Menu section in your website admin. Right now, other than viewing a few text message advising that your current themes does not need to natively help support menu, you will definitely check a few Design Location options.

Now you can make use of the GUI menus creator with this admin panel to set your current menu( s ) all together. Provide them with each and every a name, after which you can assign a menus to a location with the pull-down choices.

( You will find out your brand new navigation selections whenever you Insert a New Menus. )

Hope you all like this Tuts by, if yes then keep us supporting by sharing this post, Cheerful Coding smiley.

Submit a comment

Pin It on Pinterest