Support » Plugin: Custom Menu Wizard Widget » Some shortcode items not getting id

  • Resolved 21stcn

    (@21stcn)


    Hi there,

    I love this plugin. Thanks for all your hard work. I am using jquery to apply properties to the menu items outputted by the plugin in shortcode form. Everything works fine except some menu items don’t get an id attribute. Here is an example of the output:

    <li class="menu-item menu-item-type-post_type menu-item-object-page cmw-level-1 menu-item-419">
    <li id="menu-item-418" class="menu-item menu-item-type-post_type menu-item-object-page cmw-level-1 menu-item-418">
    <li class="menu-item menu-item-type-post_type menu-item-object-page cmw-level-1 menu-item-416">

    You can see that only 1 items gets an id. I do have multiple versions of the same menu in some content but the items not getting an ID seems random.

    I have solved my problem by using a function to get the last class from the li :
    .attr('class').split(' ').pop();

    but I am wondering if you are aware of this issue or know why it is happening as I would prefer not to have the extra code (and it is relying on the menu-item-### class being the last one)?

    https://wordpress.org/plugins/custom-menu-wizard/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author wizzud

    (@wizzud)

    Okay, from my point of view the answer is straightforward : CMW doesn’t assign the item IDs, WordPress does. However, that probably doesn’t help you very much …

    I think you will find that WordPress will only assign an ID if the ID that it intends assigning – in the format menu-item-[ID of the menu item] – has not already been used elsewhere on the page. So, for example, if you were to do jQuery(‘#menu-item-419’) on the page that produced the code pasted above, then you would get a result … but the element located would be somewhere else on the page. This is how WordPress avoids creating invalid duplicate IDs.

    PS : If you are after the “menu-item-####” from the class, have you considered using a regex? …

    x = (el.className.match(/(^|\s)(menu-item-\d+)(\s|$)/) || [0,0,''])[2];

    Thread Starter 21stcn

    (@21stcn)

    Ok, that makes sense. I have duplicate menu items on the same page so the ids not being unique sounds like the culprit.

    Now that I understand the issue better I’ll use regex to make object selection by class more robust.

    Thanks again (and for the regex)!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Some shortcode items not getting id’ is closed to new replies.