How to use a wordpress menu in php code and as a shortcode

WordPress menus provide huge benefits:
• Simple to use
• Responsive out of the box
• Possibility to automatically add new pages or posts
• Active items are highlighted

Many themes only allow to place menus at specified areas. But often it is useful to place them in individual areas.

Here are 3 different methods, have fun 🙂

1. How to place a wordpress menu in php code?

Just fill in the name that you have given your menu:

<?php wp_nav_menu( array('menu' => 'wordpress' )); ?>

My menu is named “wordpress”.
You may have to style it with CSS.

2. How to create a wordpress menu shortcode?

Generate your own shortcode for this!

Copy this code and paste it at the end of the functions.php

function print_menu_shortcode($atts, $content = null) {
extract(shortcode_atts(array( 'name' => null, 'class' => null ), $atts));
return wp_nav_menu( array( 'menu' => $name, 'menu_class' => $class, 'echo' => false ) );
}
add_shortcode('menu', 'print_menu_shortcode');

When this is done you can place your created shortcode in posts and pages.

This is the shortcode:

[menu name="worpress"]

Enter your menu name between the quotes and your done!

3. How to place a wordpress menu in content without coding?

Step1: Install the plugin “widgetize pages light” and create a widget area.
Step2: Place the wordpress default widget “custom menu” into your created widget area:
wordpress-custom-menu
Step3: Place the widget-area-shortcode (of step1) into your page or post.
Step4: Tap 3 times on your shoulder and be proud! *taptaptap*


Any questions on this? 🙂
– Kev

Leave a Reply

avatar
Kelechi
Guest

Hi, where do you paste step 1?

wpDiscuz