WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a WP filter I created that I only want to run on certain menu items.

I am attempting to use -

function add_nav_class($output) {
$output= preg_replace('/<a/', '<a class="lsbb"', $output, -1);
return $output;
}

function wp_nav_menu_conditionals() {
   if(!wp_nav_menu('theme_location' => 'footer_menu')) {
      add_filter('wp_nav_menu', 'add_nav_class');
   }
}
add_action('wp', 'wp_nav_menu_conditionals');

I want to add_filter on all menus that are not listed as theme_location footer_menu. Is there a way to do this in WP? If the menu is using theme_location footer_menu I want it to NOT use add_filter.

Can anyone tell me how to use add_nav_class conditionally?

share|improve this question

Edit -- fixed the if() statement.

function add_nav_class( $output ) {
    $menu_obj = get_term_by( 'slug', 'footer_menu', 'nav_menu' );

    // $menu_obj = get_term_by( 'name', 'Footer Menu', 'nav_menu' );
    // if the 'slug' version doesn't work, try the 'name' version
    // Just make sure you're using the correct name

    if( 'footer_menu' == $menu_obj->name ) ) {
        return $output; // bail if it's the 'footer_menu'
    }
    $output= preg_replace('/<a/', '<a class="lsbb"', $output, -1);
    return $output;
}

add_filter('wp_nav_menu', 'add_nav_class');

Edit 2 -- replaced get_term() with get_term_by().

References

share|improve this answer
    
The if statement doesn't seem to work. If there a way to confirm that you can check the wp_nav_menu? – ILikeTurtles Nov 7 '13 at 17:10
    
Updated -- try it now. – Pat J Nov 7 '13 at 17:13
    
It didn't work. My footer menu is still making the change with the filter. – ILikeTurtles Nov 7 '13 at 17:18
    
I did Print_r on the $menu_obj and it is empty. – ILikeTurtles Nov 7 '13 at 17:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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