• Hi there,

    I have a one page design template theme which works fine, except for one problem I can’t overcome.

    The template uses a custom menu manager, but the limitations in this menu made me decide to replace it with wordpress own menu (using wp_nav_menu). This works great, but because the website is a one page design it uses the anchors tag (the #) to go from page to page. The problem here is, is that I can not use the current-menu-item an current_page_item classes to highlight the active menu item. This is because wordpress adds these classes to all menu items (because of the one page design).

    The solution would be to automatically add a custom class to the active menu item. But the problem is is that I am not that prog-shaffy to come up with a code for this.

    Last couple of days I have been searching for a solution, and came across a few which worked partly. But most solutions that worked added the custom class to all menu items because they only do page checks. What I need is some sort of anchor check.

    Here is the html output of the menu:

    <div class="menu-hoofdmenu-container">
    <ul id="menu-hoofdmenu" class="menu">
    <li id="menu-item-621" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-621"><a href="https://www.3dinyourhands.com/wp_new/#home">Home</a></li>
    <li id="menu-item-592" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-592"><a href="https://www.3dinyourhands.com/wp_new/#portfolio">Portfolio</a></li>
    <li id="menu-item-594" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-594"><a href="https://www.3dinyourhands.com/wp_new/#contact">Contact</a></li>
    </ul>
    </div>

    Thanks in advance.

Viewing 6 replies - 16 through 21 (of 21 total)
  • Kurt

    (@highfxmedia)

    Martin:

    It looks like it’s working to me. I can see that when you click on a menu item, a class is set for that menu item. The class is: actief

    Add some styling to this class in your style.css file. Like this:

    .actief {
    backgound: none #CB7C2E;
    }

    I think you’re close to having it working.

    Thread Starter MrMartinH

    (@mrmartinh)

    Hey there Kurt.

    Yeah its working now…more or less.

    Got this code on the dutch forum:

    var $j = jQuery.noConflict();
    $j(document).ready(function() {
    $j('ul#menu-hoofdmenu li a').click(
        function(e) {
            e.stopPropagation();
            $j(this).closest('ul').find('.actief').removeClass('actief');
            $j(this).parent().addClass('actief');
        });
    });

    And this works. However, I now need to find a way to automatically highlight the “home” button the first time the website is loaded (since “home” is the landing page).

    Also, if you click on “nieuws” (in the footer) and than click on one of the other buttons in the menu, the item won’t be highlighted because the page is reloaded (the “nieuws” section is an external page, so that is why it reloads).

    But I have something to move on with.

    Thread Starter MrMartinH

    (@mrmartinh)

    Hm. This code:
    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Works like it should (also with the reload by switching from “nieuws” to one of the main menu items) but does only work on reload.

    This code:

    var $j = jQuery.noConflict();
    $j(document).ready(function() {
    $j('ul#menu-hoofdmenu li a').click(
        function(e) {
            e.stopPropagation();
            $j(this).closest('ul').find('.actief').removeClass('actief');
            $j(this).parent().addClass('actief');
        });
    });

    Does what it supposed to do, but then doesn’t work when the page reloads (what happens when switching from “nieuws” to one of the main menu items).

    Using those two scripts together works, but creates a lot of code. Is there a way to simplify that? (if not this solution is better then none).

    Kurt

    (@highfxmedia)

    Martin:

    I’m not a javascript expert. I’m sure someone could come up with streamlined code.

    Thread Starter MrMartinH

    (@mrmartinh)

    I am sure someone can Kurt :). No problem. Thank you for your help so far.

    Thread Starter MrMartinH

    (@mrmartinh)

    I haven’t find a proper solution yet. So if anyone can guide me in the right direction I very much like to hear it.

    The last piece of code does seem to work, hoewever, I think it will need some kind of check that compares the href attribute with what is written in the url (or viceversa). I think this might solve the refreshing problem, and that the “home” button is standard activated by switching between pages (“nieuws” page and others, since “nieuws” reloads the page).

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘wp_nav_menu, one page design anchors and highlight problem’ is closed to new replies.