If you are developing a site that uses WordPress as a CMS for part of the website and custom php pages for other sections it can be useful to utilise the functions built into WordPress on non-Wordpress pages.
One useful example is to use the WordPress menu system to manage the site’s menus. With its ability to add custom links, this offers a powerful solution to handling the complete website’s navigation.
Adding The WordPress Functions
Setting this up is actually very simple and just requires a few lines of code in the head of the page to add the necessary WordPress functions:
require('blog/wp-config.php'); $wp->init(); $wp->parse_request(); $wp->query_posts(); $wp->register_globals();
Obviously you need to change the path to the wp-config file to be the same as the directory for your WordPress installlation.
The WordPress Menu Function
To add a WordPress menu to our page we then just use the wp_nav_menu() function as we would do in our WordPress theme files. The function accepts all of the same parameters, which allows you to customise various aspects.
For our tutorial demo we are going to add the mega menu, which is set up for the WordPress mega menu plugin demo page. To identify the menu we use the “menu” parameter in the wp_nav_menu function, which accepts the menu slug name – based on the name you gave your menu in the WordPress admin system – in our case it is “mega-menu”
<?php wp_nav_menu( array( 'menu' => 'mega-menu' ) ); ?>
And that’s all there is to it – just add the function in the page where you want the menu to appear.