Posted in WordPress

Small menu fragment cache.

/**
 * Cache menu module.
 */
add_action( 'wp_update_nav_menu', function () {
	delete_transient( 'tabs-menu' );
} );

/**
 * Small menu fragment cache.
 * Cache and display Tabs menu(tabs-menu).
 */
function mx_display_tabs_menu() {
	// get from cache
	$tabs_menu_query = get_transient( 'tabs-menu' );
	// if menu isset in cache?
	if( $tabs_menu_query === false ) {
		if ( has_nav_menu( 'tabs-menu' ) ) {
			// get menu
			$tabs_menu_query = wp_nav_menu( array(
				'theme_location'=> 'tabs-menu', 
				'menu_class' 	=> 'tabs-menu',
				'echo' 		=> 0
			) );
		}
		// cache menu
		set_transient( 'tabs-menu', $tabs_menu_query, 60*60*24 );
	}
	// display menu
	echo $tabs_menu_query;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s