Reputation: 841
I've been trying to turn the following line of PHP into a shortcode in my functions.php so that I can add it to a Wordpress page.
<?php wp_nav_menu( array( 'theme_location' => 'header' ) ); ?>
After some research I found the code below for creating shortcodes, but I'm not sure how to add the line of PHP above to it to it, or even if it is the correct way to do this.
function menu_shortcode($atts, $content = null){
return '<nav class="main-menu-header">' . do_shortcode($content) . '</nav>';
}
add_shortcode('nav', 'menu_shortcode');
I initially assumed I was to add the code between the brackets of do_shortcode
instead of having $content
, but this gave me an error.
Any help would be appreciated very much.
Upvotes: 0
Views: 2573
Reputation: 4356
According to what i understand from your above question this will work for you. Add it to your functions.php and then use [nav] to display it.
function menu_shortcode( $atts ) {
return wp_nav_menu( array( 'theme_location' => 'header', 'echo' => false ) );
}
add_shortcode( 'nav', 'menu_shortcode' );
Upvotes: 1