Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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? –  Aaron 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. –  Aaron Nov 7 '13 at 17:18
    
I did Print_r on the $menu_obj and it is empty. –  Aaron Nov 7 '13 at 17:24
add comment

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.