Use walker for specific menu, or avoid use of walker

I’m trying to solve a particular problem and having trouble figuring out which way is best. So, I have a menu (generated through WP’s menu creator) and would like to pull the featured image of each chapter as a background image for its respective sub-menu. Here’s what I have right now:

/***** Images in main TOC ****/

add_filter( ‘wp_nav_menu_objects’,’add_menu_images’, 10, 2 );

function add_menu_images( $items, $args ) {

if ($args->menu == “Main_TOC”) {
$img_array = array();
foreach ( $items as $item ) {
if (!$item->menu_item_parent) {
$img_url = get_the_post_thumbnail_url($item->object_id);
$args->items_wrap = ‘<ul id=%1$s class=%2$s style=”background-image: url(‘ . $img_url . ‘)”>%3$s</ul>’;

} // end if main TOC

return $items;

Obviously this doesn’t work in the presence of more than one chapter – but I don’t know how to wrap a sub-menu instead of the whole menu without using a walker, and if I use a walker I don’t know how to make sure the code only applies to the specific menu, since the advice I’ve seen seems always to involve modifying how wp_nav_menu is called everywhere. Is it possible, within this function, to tell the “Main_TOC” menu (only) to use a custom walker? Are there other ways to apply the background-image property to submenu uls? (Getting and matching up the images probably wouldn’t be an issue, I figure I’d use arrays, but I don’t know how to get at the submenus to begin with.) And if using a walker is the only solution, and can be applied only to the menu I want to use it for, what would the walker code need to be?

Thank you!

Read more here:: Use walker for specific menu, or avoid use of walker

Leave a Reply

Your email address will not be published. Required fields are marked *