Make WordPress Core

Changeset 51072


Ignore:
Timestamp:
06/04/2021 05:06:15 PM (3 years ago)
Author:
ryelle
Message:

Twenty Twenty-One: Check for navigation element before using it.

This prevents a javascript error in case the primary navigation has been removed, for example in a child theme.

Props sushmak.
Fixes #52773.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentytwentyone/assets/js/primary-navigation.js

    r49854 r51072  
    9797    var navMenu = function( id ) {
    9898        var wrapper = document.body, // this is the element to which a CSS class is added when a mobile nav menu is open
    99             mobileButton = document.getElementById( id + '-mobile-menu' );
     99            mobileButton = document.getElementById( id + '-mobile-menu' ),
     100            navMenuEl = document.getElementById( 'site-navigation' );
     101
     102        // If there's no nav menu, none of this is necessary.
     103        if ( ! navMenuEl ) {
     104            return;
     105        }
    100106
    101107        if ( mobileButton ) {
     
    168174        } );
    169175
    170         document.getElementById( 'site-navigation' ).querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) {
     176        navMenuEl.querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) {
    171177            li.addEventListener( 'mouseenter', function() {
    172178                this.querySelector( '.sub-menu-toggle' ).setAttribute( 'aria-expanded', 'true' );
Note: See TracChangeset for help on using the changeset viewer.