I'm trying to apply a class of 'active' to the current page's corresponding menu item. I've never had this issue before/have spent hours reading similar questions to no avail and I assume it has something to do with using Wordpress's esc_url for my menu links but I can't figure out why this isn't working... My jQuery can't seem to match the urls. When I'm on Page1 I want to apply a class of active to the corresponding li and so on and so forth. This is a multi-page website. Any ideas? I'm using FoundationPress, a Wordpress boilerplate for Foundation.
<div class="top-bar" id="main-menu">
<div class="mobile-bar mobile">
<ul class="menu vertical dropdown text-center" data-dropdown-menu>
<li><a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home</a></li>
<li><a href="<?php echo esc_url( home_url( '/Page1' ) ); ?>">Page 1</a></li>
<li><a href="<?php echo esc_url( home_url( '/Page2' ) ); ?>">Page 2</a></li>
<li><a href="<?php echo esc_url( home_url( '/Page3' ) ); ?>">Page 3</a></li>
<li><a href="<?php echo esc_url( home_url( '/Page4' ) ); ?>">Page 4</a></li>
</ul>
</div>
</div>
<style>
.menu > li {
display: inline-block;
padding:0 1em;
}
</style>
<script>
$(document).foundation();
var href = location.href;
var pgurl = href.substr(href.lastIndexOf('/') + 1);
// match all the anchors on the page with the html file name
$('.menu a[href="' + pgurl + '"]').addClass('active');
</script>