Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Peter Cusack
Peter Cusack
2,622 Points

How to change what the <a> surrounds in a WordPress nav menu

I am creating a custom theme in wordpress and everything is going great except for the nav menu. By default in wordpress the link only surrounds the text of the nav item instead of the entire <li>. I have a ::before element that slides across the menu item when you hover on it. So when the element slides across it the link no longer works. I know it is possible to move what the anchor tag covers with a walker menu but I'm struggling what I need to change in my custom walker.

I'm pretty sure I have to edit something in this block but not exactly sure what.

$item_output = $args->before;
    $item_output .= '<a'. $attributes .'>';
    /* This filter is documented in wp-includes/post-template.php */
    $item_output .= $args->link_before . apply_filters( 'menu-item-' , $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</a>';
    $item_output .= $args->after;

2 Answers

Hi Peter,

Not sure if I'm understanding correctly but this video in the How to Build a WordPress theme course may help.

Around the 4 minute mark Zac discusses how to strip the tags, etc. from the navigation links.

Apologies if I've misunderstood what you need.

-Rich

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

No, we don't get into the walker class in any of our videos and it's not something I've explored much to be able to offer much help with unfortunately.