Highlight Current Link in WP Menus

Why Highlighting Active Menu Items is Important for Your Website

Highlighting the active menu item on your website’s main menu is essential for improving usability. It helps visitors to know exactly which page they are viewing and makes it easier for them to browse the menu from the current page. In the past, highlighting the current menu item based on the page a visitor was on was more labor-intensive, but with the introduction of the new drag-and-drop menu system in WordPress version 3, there are now special classes assigned to each link, making it easier to style them.

Basic WordPress Menu Output

Let’s take a moment to look at the basic WordPress Menu Output (based on a simple menu using the Twenty Ten WordPress Theme) so you can see the type of classes added automatically to the links:

Highlighting Current Pages/Category/Other Menu Links

If you take a look at the code above, you can see all the different classes WordPress adds to the menu links, but the ones you want to focus on for highlighting are the following:

1. .current-menu-item: Class added to the menu item for the current page being viewed by the user.

Example: If you are on the “about” page and there is a link to the “about” page, then it will inherit this class.

2. .current-page-ancestor: Class added to the menu item for the parent page if a child page is currently being viewed.

Example: If you have a page called “Pages” and a child page called “about,” if you are on the “about” page, then the menu link called “Pages” will inherit this class.

3. .current-post-ancestor: Class added to the menu item as long as the category is an ancestor of the post being viewed.

Example: Class is added to the category “Movie” in the menu if you are currently viewing the post called “Harry Potter,” which is in such category.

Basic Sample Menu Highlight

This is a very basic and general highlighting method, but it can get you off to a good start. Just copy and edit the following CSS in your style.css file to highlight your menu links:

.current-menu-item a,

.current-page-ancestor a,

.current-post-ancestor a { background: #000; }

Of course, this CSS targets ALL the current classes, which means if you have multiple menus on the site or even menus in your widget areas, they will be targeted. So be sure to tweak the CSS to match your theme so it only targets the menu you want to target specifically.

In conclusion, highlighting active menu items is an important aspect of website usability. It helps visitors to navigate your site more easily and improves their overall experience. With the new drag-and-drop menu system in WordPress version 3, highlighting current menu items has become much easier. By using the classes added by WordPress, you can easily style your menu links to highlight the current page, parent page, or post category.

Stay in Touch

spot_img

Related Articles