Created
August 31, 2016 18:58
-
-
Save jnschrag/025b1c76b88b7e6034eb7ed181448400 to your computer and use it in GitHub Desktop.
Include featured images of posts and pages in WordPress menus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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