Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created July 21, 2012 20:29
Show Gist options
  • Save chrisguitarguy/3157060 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/3157060 to your computer and use it in GitHub Desktop.
Add a WordPress nav menu in content with a shortcode. Helpful for HTML sitemaps and the like
<?php
/*
Plugin Name: Nav Menu Shortcode
Plugin URI: http://christopherdavis.me
Description: Add a shortcode to display a given nav menu
Version: 1.0
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action('init', 'cd_mei_add_shortcode');
/**
* Adds the shortcode `list_menu`
*
* @since 1.0
* @uses add_shortcode
* @return null
*/
function cd_mei_add_shortcode()
{
add_shortcode(
'list_menu',
'cd_mei_shortcode_cb'
);
}
/**
* Callback function for the `list_menu` shortcode. Basically a thin wrapper
* around `wp_nav_menu` that spits out the items.
*
* @since 1.0
* @uses wp_parse_args
* @uses wp_nav_menu
* @param array $args The associative array of shortcode args passed into
* the function
* @return string The menu
*/
function cd_mei_shortcode_cb($args)
{
$args = wp_parse_args($args, array(
'menu' => 'sitemap',
'class' => 'sitemap-container',
'container' => 'div'
));
return wp_nav_menu(array(
'fallback_cb' => false,
'menu' => $args['menu'],
'container' => $args['container'],
'container_class' => $args['class'],
'echo' => false
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment