Skip to content

Instantly share code, notes, and snippets.

@joanna-s
Created November 10, 2018 17:33
Show Gist options
  • Save joanna-s/fcb48b43c0aa5470f0c7e9c76dbe2f11 to your computer and use it in GitHub Desktop.
Save joanna-s/fcb48b43c0aa5470f0c7e9c76dbe2f11 to your computer and use it in GitHub Desktop.
/**
* Add featured image thumbnails to children of menu items with class menu-thumbs
*
* @param array $items List of menu objects (WP_Post).
* @param array $args Array of menu settings.
* @return array
*/
function add_images_to_special_submenu( $items ) {
$special_menu_parent_ids = array();
foreach ( $items as $item ) {
if ( in_array( 'menu-thumbs', $item->classes, true ) && isset( $item->ID ) ) {
$special_menu_parent_ids[] = $item->ID;
}
if ( in_array( $item->menu_item_parent, $special_menu_parent_ids ) && has_post_thumbnail( $item->object_id ) ) {
$item->title = sprintf(
'%1$s %2$s',
get_the_post_thumbnail( $item->object_id, 'thumbnail', array( 'alt' => esc_attr( $item->title ) ) ),
$item->title
);
}
}
return $items;
}
add_filter( 'wp_nav_menu_objects', 'add_images_to_special_submenu' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment