I'm setting up a theme for WordPress , and I need to add an active class in anchors , it works as follows:
I retrieve the URI from the current page and compare it to the text / value contained within the anchor that is automatically fetched by the WP itself's wp_list_pages( 'title_li=' )
function. If the condition returns true , it adds the active class, follows the complete code.
HTML:
<aside class="sidebar manual">
<ul class="sidebar list">
<?php wp_list_pages( 'title_li=' ); ?>
</ul>
</aside>
JavaScript:
var uri = window.location.pathname.substring(1);
var sidebar = $('.sidebar.manual');
uri.replace('/', '');
sidebar.find('.sidebar.list li > a').each(function() {
var linkValueTreated = $(this).text().split(' ').join('-').toLowerCase();
if (uri == linkValueTreated || uri == '') {
if (uri == '') uri = linkValueTreated;
$(this).text(linkValueTreated).attr('class', 'active');
}
});