Skip to content

Instantly share code, notes, and snippets.

@jnschrag
Created August 31, 2016 18:58
Show Gist options
  • Save jnschrag/025b1c76b88b7e6034eb7ed181448400 to your computer and use it in GitHub Desktop.
Save jnschrag/025b1c76b88b7e6034eb7ed181448400 to your computer and use it in GitHub Desktop.
Include featured images of posts and pages in WordPress menus
add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);
function ad_filter_menu($sorted_menu_objects, $args) {
if ($args->menu->slug != 'home-page-slider')
return $sorted_menu_objects;
// edit the menu objects
foreach ($sorted_menu_objects as $menu_object) {
// searching for menu items linking to posts or pages
// can add as many post types to the array
if ( in_array($menu_object->object, array('post', 'page', 'any_post_type')) ) {
// set the title to the post_thumbnail if available
// thumbnail size is the second parameter of get_the_post_thumbnail()
$menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'thumbnail') : $menu_object->title;
}
}
return $sorted_menu_objects;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment