Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created February 18, 2014 01:39
Show Gist options
  • Save tommcfarlin/a049b1532141a126d158 to your computer and use it in GitHub Desktop.
Save tommcfarlin/a049b1532141a126d158 to your computer and use it in GitHub Desktop.
[WordPress] How to programmatically set a default menu in WordPress.
<?php
/**
* Provides a default menu featuring the 'Home' link, if not other menu has been provided.
*
* @package Acme
* @subpackage inc
* @version 1.0.0
* @since 1.0.0
*/
function acme_default_menu() {
$html = '<ul id="acme-default-menu">';
$html .= '<li class="menu-item menu-item-type-post_type menu-item-object-page">';
$html .= '<a href="' . esc_url( home_url() ) . '" title="' . __( 'Home', 'acme' ) . '">';
$html .= __( 'Home', 'acme' );
$html .= '</a>';
$html .= '</li>';
$html .= '</ul>';
echo $html;
} // end acme_default_menu
<?php
wp_nav_menu(
array(
'theme_location' => 'main',
'fallback_cb' => 'acme_default_menu'
)
);
<?php
wp_nav_menu(
array(
'theme_location' => 'main'
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment