Skip to content

Instantly share code, notes, and snippets.

@tiborp
Last active February 3, 2022 09:53
Show Gist options
  • Save tiborp/9711443 to your computer and use it in GitHub Desktop.
Save tiborp/9711443 to your computer and use it in GitHub Desktop.
Add span tag around WordPress menu item
<?php
/**
* Add span to menu items with icon class so we can hide the text and show icons instead
*
* @link http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items
*/
add_filter( 'walker_nav_menu_start_el', 'hrt_span_to_nav_menu', 10, 4 );
function hrt_span_to_nav_menu( $item_output, $item, $depth, $args ) {
if ( isset( $item->classes ) && !empty( $item->classes ) ) {
if ( in_array( 'icon-twitter', (array) $item->classes ) || in_array( 'icon-linkedin', (array) $item->classes ) ) {
$item_output = '<span><a href="'. $item->url .'" title="'. $item->attr_title .'"></a></span>';
}
}
return $item_output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment