Make WordPress Core

Opened 8 years ago

Closed 3 years ago

Last modified 3 years ago

#15633 closed enhancement (fixed)

Add class to custom menu item when menu url is found in current page

Reported by: elBradford Owned by: celloexpressions
Milestone: 3.3 Priority: normal
Severity: normal Version: 3.0.2
Component: Menus Keywords: close
Focuses: Cc:


I found the need to identify custom nav menu items when the page is at a certain URL. For example,

Menu URL: http://mysite.com/wiki/ Page URL: http://mysite.com/wiki/Moose_Attacks

Anything below http://mysite.com/wiki/ is considered to be "within" http://mysite.com/wiki/ so I want to add a class to the menu item whenever that happens.

I made a rough patch with the following code, added just below line 381 of wp-includes/nav-menu-template.php in wordpress 3.0.2:

     if ( strpos($current_url, untrailingslashit($item_url)) == 0 )
          $classes[] = 'current_url_parent';

This probably brings up other problems, such as other custom menu items having that class, but I hope this can be considered. Thanks


Change History (8)

#1 @elBradford
8 years ago

I had to change my patch a little bit. Replace lines 380-381 with:

if ( untrailingslashit($item_url) == home_url() )
	$classes[] = 'menu-item-home';
else if ( strpos($current_url, untrailingslashit($item_url)) === 0 )
	$classes[] = 'current-url-parent';

I had to do the else-if because my home page item was being triggered all the time.

#2 @nacin
8 years ago

  • Component changed from General to Menus
  • Keywords needs-refresh added; custom menu removed
  • Milestone changed from Awaiting Review to Future Release
  • Owner set to filosofo
  • Status changed from new to reviewing

Needs a formal patch.

#3 @chriscct7
4 years ago

  • Keywords needs-patch added; needs-refresh removed

#4 @celloexpressions
4 years ago

Either current-menu-item or current-page-item checks based on the URL I think, so if someone can confirm that we can close this.

#5 @wonderboymusic
3 years ago

  • Milestone changed from Future Release to 4.4
  • Owner changed from filosofo to celloexpressions

#6 @tyxla
3 years ago

  • Keywords close added; needs-patch removed

Confirmed. In case the current URL matches the URL of a custom menu item, the current-menu-item class is added to that item.

That can be observed in _wp_menu_item_classes_by_context() in /wp-includes/nav-menu-template.php:


So I agree with @celloexpressions on closing this ticket.

#7 @wonderboymusic
3 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

Origins in [18746]

#8 @wonderboymusic
3 years ago

  • Milestone changed from 4.4 to 3.3
Note: See TracTickets for help on using tickets.