Forums

Adding Class current_page_item to main menu using Javascript (2 posts)

  1. LMcGhee
    Member
    Posted 1 year ago #

    Hi!

    I am trying to use javascript to make a page entitled Projects "active" when on each the single post page.

    I am placing this code on the top of the single.php before it gets the header (which includes the project link).

    <script>
    jQuery(document).ready(function(){
    $("#menu-item-19").children("a").addClass("current_page_item");
    });
    </script>

    Where #menu-item-19 is the id of the li that includes the link to the projects page.

    Any idea why the link isn't getting the class "current_page_item"?

    Thanks in advance!

  2. Devin Price
    Member
    Posted 5 months ago #

    It should work. WordPress applies the class to the "li" though, rather than the "a", so perhaps that's where the conufsion was.

    $('#menu-item-19').addClass('current_page_item');

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.