0

I'm attempting to add thumbnails to navigation items as per this question. However, after adding the sample code to the top of my functions.php file nothing happens. I've whittled the code down in an attempt to get anything to happen. The code is now this:

add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);

function ad_filter_menu($sorted_menu_objects, $args) {
    return '';
}

Still nothing is happening. The menu appears absolutely normally.

Heres the code thats generating the menu in header.php:

<?php wp_nav_menu( array( 'theme_location' => 'main-menu' ) ); ?>

What am I missing?

1
+50

First:

Your ad_filter_menu is a filter function: it filters/manipulates $sorted_menu_objects That's why you always have to return the $sorted_menu_objects, and return ''; won't work.

Second:

A better way to achieve the desired behavior (adding thumbnails to navigation) is to extend the Walker_Nav_Menu class

There are several guides and templates out there that fix this for you, see for example this previous question

  • The return '' line was simply a way to test that the code is doing anything at all. That line should make the nav display nothing at all. Am I incorrect in thinking this? – Nathan Arthur Sep 1 '15 at 14:59
  • Yes that is incorrect. Instead you can do return $sorted_menu_objects = array(); – Richard Sep 3 '15 at 7:09
  • Thanks for pointing out I should be using a custom walker! While the answer you pointed out seems quite outdated, it's gotten me well on my way to having a functioning thumbnail-enabled navigation. – Nathan Arthur Sep 7 '15 at 17:16
1

I can only suggest that you've got a 3rd party plugin or theme modification that is interfering somehow. I've tested your code exactly in several different installs and it works as expected.

A few other things to consider:

  • Is your environment behind a cache of some sort?
  • Are you working in a child theme?
  • Do you have another plugin installed that is modifying or building the menus?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.