Dynamic wordpress menu

Register wordpress menu

add_action('init', 'my_theme_register_menu');
function my_theme_register_menu() {
	register_nav_menu( 'main-menu', 'Main Menu');
} 

// Default menu

function my_theme_default_menu() {
    echo '<ul id="nav">';
    if ('page' != get_option('show_on_front')) {
        echo '<li><a href="'. home_url() . '/">Home</a></li>';
    }
    wp_list_pages('title_li=');
        echo '</ul>';
}

Call menu where you need.

General:

<?php wp_nav_menu(array('theme_location' => 'main-menu', 'menu_id' => 'nav')); ?>

Dynamicaly

<?php
if (function_exists('my_theme_default_menu')) {
    wp_nav_menu(array('theme_location' => 'main-menu', 'menu_id' => 'nav', 'fallback_cb' => 'my_theme_default_menu'));
}
else {
    my_theme_default_menu();
}
?>
The following two tabs change content below.

One thought on “Dynamic wordpress menu

  1. //This code have to use in functions.php to registered menu

    /******This code for registration for wp menu**********/

    function wpj_register_menu() {

    if (function_exists(‘register_nav_menu’)) {

    register_nav_menu(
    ‘top_main_menu’, __( ‘Main Menu’, ‘theme-name’ ) ); // Here
    (wpj-main-menu) is unique id & Main Menu is display name

    // Registared Another Menu

    }

    }

    add_action(‘init’, ‘wpj_register_menu’);

    //This code have to use instead of ul li

    ‘top_main_menu’, // this id from functions.php

    ‘menu’ => ”,

    ‘container’ => ‘div’,

    ‘container_class’ => ”,

    ‘container_id’ => ”,

    ‘menu_class’ => ‘menu’,

    ‘menu_id’ => ”,

    ‘echo’ => true,

    ‘fallback_cb’ => ‘wp_page_menu’,

    ‘before’ => ”,

    ‘after’ => ”,

    ‘link_before’ => ”,

    ‘link_after’ => ”,

    ‘items_wrap’ => ‘%3$s’,

    ‘depth’ => 0,

    ‘walker’ => ”

    ));

    }

    else {

    wpj_default_menu();

    }

    ?>

    if we use by this way then will it be correct way ? would you please explain about this ?

Leave a Reply

Your email address will not be published. Required fields are marked *